我如何安排多个功能以永远在Python的不同时间范围内执行



在python 3x中,我如何在python中反复执行多个函数?

例如,我有两个功能,'funca''funcb'。我希望'funca'每17秒执行一次,而'funcb'将每13秒执行一次。

预先感谢您。

只需在循环时创建一个无限的,并保持第二个数量,使用time.sleep()暂停:

import time
t = 0
while True:
    t += 1
    time.sleep(1)
    if t % 13 == 0:
        funcB()
    if t % 17 == 0:
        funcA()

您也可以将itertools.count()for循环使用无限while

import itertools
import time
for i in itertools.count(1):
    time.sleep(1)
    if i % 13 == 0:
        funcB()
    if i % 17 == 0:
        funcA()

最新更新