为了账号安全,请及时绑定邮箱和手机立即绑定

如何取消python中的setInterval?

如何取消python中的setInterval?

肥皂起泡泡 2021-11-30 16:19:12
我正在使用此代码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() 


查看完整回答
反对 回复 2021-11-30
  • 1 回答
  • 0 关注
  • 180 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信