from threading import Timer
import time
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
)
global t #globaljj
class demo_timer:
def __init__(self):
self.__t = None
def demo_print(self):
logging.info("hello")
def demo_start(self):
if self.__t is not None:
self.__t.cancel()
logging.info("timer cancel %s",self.__t)
self.__t = None
self.__t = Timer(5,self.demo_print)
logging.info("timer %s",self.__t)
self.__t.start()
a = demo_timer()
t = Timer(5,a.demo_print)
while True:
time.sleep(0.1)
a.demo_start()
結果:Python whileタイマーでループします。 Cacelは最終的に終了し、有効になりませんでした
木、2017年6月29日8時55分59秒time_test.py [ライン:23] INFOタイマー< _timer(スレッド-1061、初期)> Thu、29 Jun 2017 08:55:59 time_test.py [行:20] INFOタイマーキャンセル< _Timer(スレッド-1061、開始15192)> Thu、29 Jun 2017 08:55:59 time_test.py [行: 2335 INFOタイマー< _Timer(Thread-1062、initial 16088)> Thu、29 Jun 2017 08:55:59 INFOタイマーキャンセル< _Timer(Thread-1062、started 16088)> Thu、 time_test.py [行:23]情報タイマー< _Timer(Thread-1063、initial)> Thu、29 Jun 2017 08:55:59 time_test.py [行:20] INFOタイマーキャンセル< _Timer(スレッド-1063、開始14860)> Thu、29 Jun 2017 08 :55:59 time_test.py [ライン:23] INFOタイマ< _timer(初期スレッド-1064)>
[完了] 114.895秒
ようこそスタックオーバーフロー!あなたの質問は非常に明確ではないので、答えが難しくなります。改善してみてください。ここにいくつかのリソースがあります:https://stackoverflow.com/help/how-to-ask and https://stackoverflow.com/help/mcve –