The easiest solution would be to use SimpleQueue as suggested by user2357112's response - but I need to maintain support for python <3.7 which doesn't include it.
Upon further reading on python reentrancy, I learned signals can only trigger between atomic operations.
Therefore I switched from queue to deque, which is O(1) and atomic on both append()
and popleft()
, and therefore also thread-safe + signal-safe.
import signal
from collections import deque
event_queue = deque()
def signal_handler(signum, frame):
event_queue.append(signum)
signal.signal(signal.SIGWINCH, signal_handler)
while True:
if event_queue:
evt = event_queue.popleft()
print(f"Got an event: {evt}")