Python C++ extension that replicates (most of the) functionality from queue.SimpleQueue
(available in Python 3.7+) and is safe to use within a Python signal handler.
I was looking for a way implement a producer-consumer queue for handling Python signals,
and didn't realize queue.SimpleQueue
was already reentrant.
If you attempt to use queue.Queue
from within a (Python) signal handler,
a deadlock is likely to result: it uses a threading.Lock
and a signal may be delivered
a second time after that lock has been acquired, causing it to attempt to acquire it again.
Starting a thread (to handle that signal) from a signal handler has the same issue, as starting it involves acquiring a lock or two.
signal_queue.SimpleQueue
makes use of the fact that Python signal handlers are only called
from within the interpreter (i.e., can't interrupt native code).
Pretty much like you'd use Python's queue.SimpleQueue
:
import threading, signal
from signal_queue import SimpleQueue
q = SimpleQueue()
def handle_signal(signum, stack):
q.put(stack)
def handler_thread():
while True:
item = q.get()
...
...
signal.signal(signal.SIGUSR1, handle_signal)
threading.Thread(target=handler_thread, daemon=True).start()
...