Robert Bradshaw schrieb am 29.09.2017 um 10:00:
I read through the code a little. Correct me if I'm wrong, but it's
essentially a replacement for PyErr_CheckSignals(), with the intention to
be faster because it uses inlined code, shared flags and counters. Looking
at the source of PyErr_CheckSignals() showed that it already uses a
(private) global atomic interrupt flag "is_tripped" to speed things up
here. The main difference is the C call overhead and the atomic flag access
in CPython. I trust Jeroen that his benchmarks have shown that that's still
too high in some cases, probably in long running tight loops.
cysignals also seems to replace all signal handlers, which suggests to me
that it can't be integrated into Cython as a general feature. It's
something that can be done at an application level, but not at a level as
low as a programming language that users commonly write libraries with.
That, IMHO, puts it out of scope for integration into Cython, even though I
understand that doing something fast requires intercepting the signals.
Regarding Cython support for signals, I'm really not convinced that
try-except is the right construct for signal handling, simply because they
are not linked to the code currently being executed.
The main problem that Cython has is that it's not clear when and how often
to check for signals. CPython has a complete infrastructure for signal
handling and uses a reasonable heuristic for signal checking based on
runtime byte code execution. That is much better than what Cython could do,
because a static compiler cannot really estimate how much "time" has passed
since the last check or if a loop is going to execute long enough to merit
injecting a signal check into its body.
Thus, it's actually most efficient to let users sprinkle their code with
explicit signal checks, but that is also easy to forget. We could make that
a tiny bit less easy by providing a Cython global "check_signals()" as an
alias that is "just there", but I don't see much we could (or should) do on
top of that. CPython pretty much does things right, and I can fully
understand it if they do not want to expose the internals of
PyErr_CheckSignals().
Stefan