Terminate a multi-thread python program

前端 未结 7 769
猫巷女王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 16:57

    thread1 = threading.Thread(target=your_procedure, args = (arg_1, arg_2))    
    try:
          thread1.setDaemon(True)  # very important
          thread1.start()
    except (KeyboardInterrupt, SystemExit):
          cleanup_stop_thread()
          sys.exit()
    

    When you want to kill the thread just use:

    thread1.join(0)
    

提交回复
热议问题