是否有可能抓住一个SIGINT来阻止Julia程序运行,但却以“有序”的方式运行呢?
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继续。是否有可能(轻轻地)尽早停止,但使用SIGINT信号?
发布于 2016-12-13 13:28:36
您只需使用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将检查异常是否为interupt,如果是,则返回值。否则就会出错。
https://stackoverflow.com/questions/41122123
复制相似问题