3 ms·
Right which is why you don't actually do much of anything in the signal handler except save that it happened somewhere to be processed by your application later
by Spivak 3y ago
Right which is why you don't actually do much of anything in the signal handler except save that it happened somewhere to be processed by your application later.
https://docs.python.org/3/library/signal.html#execution-of-python-signal-handlers https://docs.python.org/3/library/signal.html#execution-of-p...
Here's how Python does it.
- skitter 3y agoUh, that Examples section calls print inside the signal handler. Python's IO stack is non-reentrant. I hope nobody follows these examples and gets exceptions at runtime because of it.
- eesmith 3y ago"A Python signal handler does not get executed inside the low-level (C) signal handler. Instead, the low-level signal handler sets a flag which tells the virtual machine to execute the corresponding Python signal handler at a later point(for example at the next bytecode instruction)" The low-level C handler set the flag then returns. Only some time later does the Python run-time call the associated Python handler that you see in the Examples section.
- skitter 3y agoIn this case that's before the next bytecode executes. The write syscall gets interrupted and returns EINTR, then cpython checks what signal was caught and executes the signal handler, before trying to do the remaining write: https://github.com/python/cpython/blob/5f7aba938cf5007b6f95472a3a54732dd99a2df8/Modules/_io/bufferedio.c#L1809 https://github.com/python/cpython/blob/5f7aba938cf5007b6f954... Here's even a test for it: https://github.com/python/cpython/blob/5f7aba938cf5007b6f95472a3a54732dd99a2df8/Lib/test/test_io.py#L4502 https://github.com/python/cpython/blob/5f7aba938cf5007b6f954...