简体   繁体   English

如何终止python中的计时器线程

[英]how to terminate a timer thread in python

i have a code to run a timer thread and its doing a job every x seconds and its has no problem 我有一个代码来运行计时器线程,并且每隔x秒执行一次工作,并且没有问题

import threading
import datetime

def starThread():
    t = threading.Timer(0, Do_Analysis())
    t.start()

def Do_Analysis(): 
    #define  threading.Timer to do the job every x secound
    t=threading.Timer(1, Do_Analysis).start()
    print('datetime is : ',datetime.datetime.now())
    print(threading.active_count)

starThread()

i need to terminate the thread at some point and i dont know how to do it can anyone please guide me 我需要在某个时候终止线程,我不知道该怎么做,任何人都可以指导我

Do something like this: 做这样的事情:

t=threading.Timer(1, Do_Analysis)
t.start()
if True:
    t.cancel()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM