小编Man*_*pez的帖子

如何杀死这个threading.Timer?

我已经使用一个Timer对象启动了一个线程.现在,我想停止这个线程,但我不能.我用过cancel(),但它不起作用.我不知道为什么.

import threading
import time
import sys as system
def Nop():
    print("do nothing")
    return 0
def function():
    try:
        while True:
            print("hello world ")
            time.sleep(2)
    except KeyboardInterrupt:
            print( "Good job!! exception catched") 
t = threading.Timer(10, function)
t.start()
print(t.getName)
counter = 0
while True:
    try:
        time.sleep(1) 
        Nop()
        counter = counter +1
        print(counter)
        if counter == 20:
            print(t.getName)
            t.cancel()
            counter = 0
            break
        if t.is_alive() == False:
            print("The Timer thread is dead...")
    except KeyboardInterrupt:
        print("End of program")
        t.cancel()
        system.exit(0) …
Run Code Online (Sandbox Code Playgroud)

python multithreading timer

2
推荐指数
1
解决办法
7584
查看次数

标签 统计

multithreading ×1

python ×1

timer ×1