使用thread.join()

时间:2019-03-24 19:38:57

标签: python multithreading join keyboardinterrupt

我有多个线程,并且每个线程都按顺序运行join方法。但是当给出KeyboardInterrupt时,执行不会终止。有办法解决吗?

EXIT = False
def log_status():
    global EXIT
    while not EXIT:
        print "Sleeping"
        sleep(5)
        print  "done sleeping"
        // Log info to file

def clean_up():
    global EXIT
    while True:
        inp = raw_input("Enter ENTER to stop the script:\n")
        if inp == '':
            EXIT = True
            break

## Main function
    t1 = MyThread(target=log_status)
    t2 = MyThread(target=clean_up)
    t1.start()
    t2.start()
    t1.join()
    t2.join()

在这里,当执行t1.join()并给出KeyboardInterrupt时,脚本执行不会退出。我看到了一些建议在联接中使用较小的超时时间,但是我不想这样做。给出KeyboardInterrupt后,还有其他方法可以停止脚本执行吗?

0 个答案:

没有答案