Terminate a multi-thread python program

前端 未结 7 756
猫巷女王i
猫巷女王i 2020-12-02 16:17

How to make a multi-thread python program response to Ctrl+C key event?

Edit: The code is like this:

import threading
current = 0

c         


        
相关标签:
7条回答
  • 2020-12-02 17:15

    If you spawn a Thread like so - myThread = Thread(target = function) - and then do myThread.start(); myThread.join(). When CTRL-C is initiated, the main thread doesn't exit because it is waiting on that blocking myThread.join() call. To fix this, simply put in a timeout on the .join() call. The timeout can be as long as you wish. If you want it to wait indefinitely, just put in a really long timeout, like 99999. It's also good practice to do myThread.daemon = True so all the threads exit when the main thread(non-daemon) exits.

    0 讨论(0)
提交回复
热议问题