2016-12-13 1 views
1

Julia 프로그램이 실행되는 것을 막기 위해 SIGINT를 잡아낼 수 있습니까? "순서대로"유행합니까? JuliaLang의 SIGINT 처리

function many_calc(number) 
    terminated_by_sigint = false 
    a = rand(number) 
    where_are_we = 0 
    for i in eachindex(a) 
     where_are_we = i 
     # do something slow... 
     sleep(1) 
     a[i] += rand() 
    end 
    a, where_are_we, terminated_by_sigint 
end 

many_calc(100) 

내가 너무 오래 걸릴 것 실현하지 못했지만, 모든 결과를 버리고 싶지 않기 때문에 나는 where_are_we-1에서 계속 다른 방법을 가지고 있기 때문에 내가 30 초 efter을 종료 할 말 . SIGINT 신호를 사용하여 일찍 (부드럽게) 멈출 수 있습니까?

답변

2

try ... catch ... end을 사용하고 오류가 인터럽트인지 확인할 수 있습니다. 코드에 대한

:

function many_calc(number) 
    terminated_by_sigint = false 
    a = rand(number) 
    where_are_we = 0 
    try 

     for i in eachindex(a) 
      where_are_we = i 
      # do something slow... 
      sleep(1) 
      a[i] += rand() 
     end 

    catch my_exception 
     isa(my_exception, InterruptException) ? (return a, where_are_we, true) : error() 
    end 

    a, where_are_we, terminated_by_sigint 
end 

예외가 매치를 중지 있는지 확인 것인가, 만약 그렇다면 값으로 돌아갑니다. 그렇지 않으면 오류가 발생합니다.

+0

간단하고, 그것이 가능하다는 것을 알지 못했습니다. 감사 – pkofod