在python中停止计时器

时间:2015-10-21 00:30:03

标签: python timer

我制作了一个小插件程序,在我的Ubuntu终端上输出一堆随机数,直到数字满足某个条件,此时打印DoCmd.RunSQL "select * from Rule where ChangedFlag = 1 and Years = " + _ years2 + " and Months = " + months2 + " and CountyID = " + countyID2 + "" DoCmd.RunSQL "insert into Rule values (Years, Months, CountyID) Values " & _ " (years2, months2, countyID2)"

然而,一旦满足条件,我想停止定时器循环,我不知道如何。

以下是代码:

access granted

2 个答案:

答案 0 :(得分:3)

Timer个对象在延迟后调用其函数一次,就是这样。这是一个"循环"是因为你告诉计时器打电话的功能是设置下一次定时呼叫的功能。所以你需要做的就是一旦满足你的条件就不要这样做:

def message():
    num = str(random.randint(137849013724,934234850490))
    print(num)
    if num[3] == "5" and num[6] == "7":
        print("access granted")
    else:
        threading.Timer(0.125, message).start()  # <== moved

答案 1 :(得分:1)

您无需为此创建线程计时器。只需延迟适当数量的随机数:

import time
import random
for i in range(10):
    print(random.randint(1,10), flush=True)
    time.sleep(0.1)