79576248

Date: 2025-04-16 01:24:28
Score: 1
Natty:
Report link

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}")
Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: ShawnM