Python threading.Timer 有时会停止或暂停或冻结

时间:2021-04-22 00:39:03

标签: python python-3.x timer python-multithreading

我的代码是这样总结的。

import threading
import time

def checkMinute(num):
    print('check ' + str(num))
    print(str(num) + ' work start')
    # other python code....
    print(str(num) + ' work end')
    startTimer(num)

def startTimer(num):
    t = threading.Timer(10, checkMinute, args=(num,))
    t.daemon = True
    t.start()
    
if __name__== '__main__':
    startTimer(1)
    startTimer(2)
    startTimer(3)
    time.sleep(1000)

它必须在 keyInturrepted 之前继续工作。

但有时,我不知道怎么说,但它会停止、暂停或冻结。

例如像这样的控制台日志

# 10am in real time. program is start
2021-04-21 10:00:00 : check 1   
2021-04-21 10:00:00 : 1 work start
2021-04-21 10:00:00 : check 2   
2021-04-21 10:00:00 : 2 work start
2021-04-21 10:00:00 : check 3   
2021-04-21 10:00:00 : 3 work start
2021-04-21 10:00:13 : 1 work end
2021-04-21 10:00:20 : 2 work end
2021-04-21 10:00:21 : 3 work end
# I want continue it
# but sometimes
2021-04-21 17:10:21 : 3 work end
# Next, 'check 1' and '1 work start' have to progress
# But the real time is 9pm, but no progress has been made since the program stopped at 5pm.
# When I input enter on command window, It work again with no error

为什么会这样? 我该如何解决?

python 版本是 3.6。

0 个答案:

没有答案