如何杀死这个threading.Timer? [英] How to kill this threading.Timer?

查看:109
本文介绍了如何杀死这个threading.Timer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have launched a thread using a Timer object. Now, I want to stop this thread but I can't. I have used cancel(), but it doesn't work. I don't know why.

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)
    except:
        print("something wrong happens...")
if t.is_alive() == True:
    print("timer is alive...")
    print(t.getName)
    t.cancel()
print("final")
system.exit(0)  

当计数器等于20时,线程应停止,但它仍然处于活动状态.当我退出while循环时,还有另一项检查.计时器还活着,是同一个线程,我尝试取消,但不起作用.

The thread should stop when counter is equal to 20, but it is still alive. When I am out of the while loop, there is another check. The Timer is alive, is the same thread, I try to cancel, but it doesn't work.

有什么想法吗?

do nothing
17
hello world 
do nothing
18
do nothing
19
hello world do nothing

20
<bound method _Timer.getName of <_Timer(Thread-6, started daemon 9916)>>
timer is alive...
<bound method _Timer.getName of <_Timer(Thread-6, started daemon 9916)>>
final
To exit: use 'exit', 'quit', or Ctrl-D.
An exception has occurred, use %tb to see the full traceback.

SystemExit: 0

hello world 
hello world 
hello world 
hello world 

推荐答案

可以通过阅读文档会更仔细.计时器线程的cancel方法"...仅在计时器仍处于等待阶段时才起作用."

The problem here can be solved by reading the documentation more carefully. A timer thread's cancel method "... will only work if the timer is still in its waiting stage."

在您调用t.cancel时,计时器已触发,并且其相关功能正在执行,因此即使该功能实际上花费了大部分时间在睡眠中,它也不再处于等待阶段"-这并不意味着它处于等待阶段,它将在计时器触发时终止.

By the time you call t.cancel the timer has fired, and its associated function is executing so it is no longer "in its waiting stage", even though the function actually spends most of its time sleeping - this does NOT mean it's in its waiting stage, which terminates when the timer fires..

更普遍的是,没有它的积极合作就无法杀死一个线程,因为 SO答案已经很好地总结了.

More generally there is no way to kill a thread without its active cooperation, as several SO answers already summarize well.

而不是使用cancel,您应该设置function所查看的某种标志,以确定是否应终止其循环(并因此终止整个线程).

Rather than using cancel you should set some sort of flag that function looks at to determine whether it should terminate its loop (and therefore the whole thread) or not.

这篇关于如何杀死这个threading.Timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆