Calling pselect/ppoll/epoll_pwait
Ian Pilcher
arequipeno at gmail.com
Tue Dec 13 13:50:34 EST 2022
On 12/2/22 14:00, Ian Pilcher wrote:
> Does Python provide any way to call the "p" variants of the I/O
> multiplexing functions?
Just to close this out ...
As others suggested, there's no easy way to call the "p" variants of the
I/O multiplexing functions, but this can be worked around by "mapping"
signals to file descriptors.
There are a few ways to accomplish this.
1. Use a Linux signalfd. There's at least one library out there that
provides signalfd support to Python.
2. Use signal.set_wakeup_fd()[1]. I didn't really explore this, as it
appears that there isn't any way to filter the signals that will be
reported.
3. Roll your own. This turned out to be really simple for my use case,
which is simply to set an exit flag and wake my program up if it
receives SIGINT or SIGTERM.
_sel = selectors.DefaultSelector()
_exit_flag = False
_sig_pipe_r, _sig_pipe_w = os.pipe2(os.O_NONBLOCK | os.O_CLOEXEC)
def _sig_handler(signum, frame):
global _exit_flag
_exit_flag = True
os.write(_sig_pipe_w, b'\x00')
_sel.register(_sig_pipe_r, selectors.EVENT_READ)
# register other file descriptors of interest
signal.signal(signal.SIGINT, _sig_handler)
signal.signal(signal.SIGTERM, _sig_handler)
while not _exit_flag:
ready = _sel.select()
# handle other file descriptors
[1] https://docs.python.org/3/library/signal.html#signal.set_wakeup_fd
--
========================================================================
Google Where SkyNet meets Idiocracy
========================================================================
More information about the Python-list
mailing list