I am trying to have a main thread wait for its worker threads to finish using the following code, but when I try to interrupt it with Ctrl+C it doesn't stop
import threading
import sys
exit = threading.Event()
#pass it to the threads
try:
exit.wait()
print('Goodbye')
sys.exit()
except KeyboardInterrupt:
print('Interrupted')
sys.exit()
UPDATE Nothing prints. All background threads are daemons.
waits onthreadingobjects should be interruptible by signals, and, once it's interrupted, Python will immediately process the SIGINT as aKeyboardInterrupt. But on Windows… I'm not sure how that works, but you may need something more.exit.wait(), and hasn't yet printedGoodbye', right? (Because otherwise, it's already left thetry/exceptblock, so obviously thetrycan't capture the exception.)Interruptedprint?