JuliaPy / JuliaPy/pyjulia

KeyboardInterrupt support / SIGINT handler

Open
#211 3 comments 5 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
893
Forks
101
PR merge metrics
No merged PRs in 30d

Description

Currently PyJulia does not support `KeyboardInterrupt`. That is to say, in long-running Python and Julia computation, there is no way to terminate a sub-computation by catching `KeyboardInterrupt` as done in normal Python programming.

Aside: Recommended way to cancel current input in REPL is to use IPython 7.0 or above. Ctrl-C would cancel the input without causing SIGINT.

This problem is previously mentioned in: https://github.com/JuliaPy/pyjulia/issues/189, https://github.com/JuliaPy/pyjulia/issues/185#issuecomment-418614274

What follows is a summary of my understanding:

When PyJulia is initialized, libjulia takes over all signal handling. [I couldn't find a way to disable this behavior](https://discourse.julialang.org/t/how-can-i-handle-sigint-when-using-libjulia-c-api-in-python-ctypes/14794). Julia documentation mentions that ["Julia requires a few signal to function property."](https://docs.julialang.org/en/latest/devdocs/debuggingtips/#Dealing-with-signals-1) so it probably would not have something like [`Py_InitializeEx`](https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx) to initialize libjulia without installing signal handlers anytime soon.

What would be more easily achievable is to let Julia translate SIGINT to `InterruptException` and then let PyCall to translate it to Python's `KeyboardInterrupt`. I have implemented it in https://github.com/JuliaPy/PyCall.jl/pull/574 but it introduced [a bug](https://github.com/JuliaPy/PyCall.jl/pull/574#issuecomment-425322221) which is hard to track. I reported it in Julia: https://github.com/JuliaLang/julia/issues/29498. Note that this strategy is not perfect because long-running pure-Python computation or I/O cannot respond to SIGINT. If Julia implements some kind of signal handling https://github.com/JuliaLang/julia/issues/14675 then maybe we can call [`PyErr_SetInterrupt`](https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetInterrupt) (which does not need GIL) from it.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.