Python 如何使用threading.timer启动-暂停-继续-暂停-继续-暂停?
如何使用threading.timer启动-暂停-继续-暂停-继续-暂停 到目前为止我所做的:Python 如何使用threading.timer启动-暂停-继续-暂停-继续-暂停?,python,Python,如何使用threading.timer启动-暂停-继续-暂停-继续-暂停 到目前为止我所做的: class Scheduling(threading.Thread): def __init__(self): self.schedule = None threading.Thread.__init__(self) def run(self): s
class Scheduling(threading.Thread):
def __init__(self):
self.schedule = None
threading.Thread.__init__(self)
def run(self):
self.schedule = threading.Timer(2, self.run).start()
总的来说,我有这样的电话:
sched = Scheduling()
sched.start()
我已经写了def fin(self):self.schedule.cancel(),但它没有停止为什么
您的self.schedule对象不是线程。计时器对象。start()方法。您需要显式地创建计时器
class Scheduling(threading.Thread):
def __init__(self):
self.schedule = None
threading.Thread.__init__(self)
def run(self):
self.schedule = threading.Timer(2, self.run)
self.schedule.start()
def cancel(self):
self.schedule.cancel()
我没有测试它。然而,我不确定,你想要定时器做什么。两秒钟后,它将调用run重新启动新计时器?正在启动和暂停的是什么?主线?一条不同的线?还有别的吗?该如何继续?@HenryGomersall threading.timer要暂停计时器吗?不容易忘记。你到底想达到什么目的?@HenryGomersall如果这很棘手,我怎么才能杀死或取消计时器?计时器可以用“.cancel()”停止。T