朱莉娅,处理键盘中断

alt*_*lto 5 sigint julia

标题说明了一切.如何处理或抓住SIGINT朱莉娅?从我假设的文档中我只想InterruptException使用如下的try/catch块来捕获

try
    while true
        println("go go go")
    end
catch ex
    println("caught something")
    if isa(ex, InterruptException)
        println("it was an interrupt")
    end
end
Run Code Online (Sandbox Code Playgroud)

但是当我用程序杀死程序时,我永远不会进入catch块^C.

编辑:上面的代码按照julia REPL的预期工作,而不是在脚本中.

Mik*_*son 8

我看到了与alto相同的行为,即SIGINT当我的代码作为脚本运行时会杀死整个进程,但是当它在REPL中运行时会被捕获为错误.我的版本是最新的,看起来非常类似于tholy:

julia> versioninfo()
Julia Version 0.3.7
Commit cb9bcae* (2015-03-23 21:36 UTC)
Platform Info:
  System: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Core(TM) i7-3610QM CPU @ 2.30GHz
  WORD_SIZE: 64
  BLAS: libopenblas (DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas
  LIBM: libopenlibm
  LLVM: libLLVM-3.3
Run Code Online (Sandbox Code Playgroud)

通过源代码挖掘,我发现Julia's中断行为由一个jl_exit_on_sigint选项决定的提示,可以通过一个选项来设置ccall. jl_exit_on_sigint0用于REPL,但它看起来好像是从命令行运行程序文件时init.c设置的.1Julia

ccall无论调用环境如何,添加适当的使alto的代码都有效:

ccall(:jl_exit_on_sigint, Void, (Cint,), 0)

try
    while true
        println("go go go")
    end
catch ex
    println("caught something")
    if isa(ex, InterruptException)
        println("it was an interrupt")
    end
end
Run Code Online (Sandbox Code Playgroud)

这似乎有点像黑客.是否有更优雅的方式来选择环境的中断行为?默认似乎很明智,但也许应该有一个命令行选项来覆盖它.