def _wait_forever(self):
# Wait forever
while True:
# Check if signals have been received
if os.name == "posix":
self._empty_signal_pipe()
self._run_signal_handlers()
if os.name == "posix":
# NOTE(sileht): we cannot use threading.Event().wait(),
# threading.Thread().join(), or time.sleep() because signals
# can be missed when received by non-main threads
# (https://bugs.python.org/issue5315)
# So we use select.select() alone, we will receive EINTR or
# will read data from signal_r when signal is emitted and
# cpython calls PyErr_CheckSignals() to run signals handlers
# That looks perfect to ensure handlers are run and run in the
# main thread
try:
select.select([self.signal_pipe_r], [], [])
except select.error as e:
if e.args[0] != errno.EINTR:
raise
else:
# NOTE(sileht): here we do only best effort
# and wake the loop periodically, set_wakeup_fd
# doesn't work on non posix platform so
# 1 seconds have been picked with the advice of a dice.
time.sleep(1)
# NOTE(sileht): We emulate SIGCHLD, _service_manager
# will just check often for dead child
self._signals_received.append(SIGCHLD)
评论列表
文章目录