我正在使用此代码doSomething每 3 秒调用一次函数。但我想要那个时候numeroPeticiones== 3,停止这样做。我找了很多例子,但没有一个有效。就我而言,即使numeroPeticiones== 3满足的条件,我的函数仍将永远执行。我该如何解决?import threadingfrom threading import TimernumeroPeticiones=0def doSomething(): global numeroPeticiones numeroPeticiones=numeroPeticiones+1 print ("hello, world",numeroPeticiones) if numeroPeticiones == 3: print("cancel") t.cancel()def set_interval(func, sec): def func_wrapper(): set_interval(func, sec) func() t = threading.Timer(sec, func_wrapper) t.start() return t
1 回答

明月笑刀无情
TA贡献1828条经验 获得超4个赞
您可以执行以下操作,如果 num 等于 3,则不会启动计时器
from threading import Timer
num = 0
def hello():
global num
num += 1
print("hello, world")
if (num < 3):
Timer(3, hello).start()
Timer(3, hello).start()
添加回答
举报
0/150
提交
取消