Skip to content

Instantly share code, notes, and snippets.

@romuald
Created January 7, 2014 10:53
Show Gist options
  • Save romuald/8297748 to your computer and use it in GitHub Desktop.
Save romuald/8297748 to your computer and use it in GitHub Desktop.
Did I just find a bug in python locks? Or do I simply don't understands them? Signals does not wake up main thread if waiting with an indefinite time
"""
When `wait` is None, program does not receive signal (and cannot be stopped)
When anything else, signals wakes up main thread and stop as intended
"""
import os
import signal
from threading import Event
lock = Event()
def sighandler(signum, frame):
print 'got signal %d' % signum
lock.set()
signal.signal(signal.SIGTERM, sighandler)
signal.signal(signal.SIGINT, sighandler)
print 'Start with pid %d' % os.getpid()
wait = 10
while not lock.wait(wait):
print 'waited %r seconds' % wait
print 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment