The following program hangs the terminal such that it ignores Ctrl+C. This is rather annoying since I have to restart the terminal every time one of the threads hang.
Is there any way to catch the KeyboardInterrupt while waiting on an event?
import threading
def main():
finished_event = threading.Event()
startThread(finished_event)
finished_event.wait()#I want to stop the program here
print('done!')
def startThread(evt):
"""Start a thread that will trigger evt when it is done"""
#evt.set()
if __name__ == '__main__':
main()
Update: On the current Python 3
finished_event.wait()works on my Ubuntu machine (starting with Python 3.2). You don’t need to specify thetimeoutparameter, to interrupt it using Ctrl+C. You need to pass thetimeoutparameter on CPython 2.Here’s a complete code example:
There could be bugs related to Ctrl+C. Test whether it works in your environment.
Old polling answer:
You could try to allow the interpreter to run the main thread:
If you just want to wait until the child thread is done: