如何在Python中每2秒调用一次函数? [关闭]

问题描述 投票:-2回答:1

我想弄清楚如何在Python中每2秒调用一次函数?我应该使用计时器还是线程?有人可以提供链接/示例吗?

python python-3.x
1个回答
1
投票

sched模块可以做到这一点:

import sched, time
schedTimer = sched.scheduler(time.time, time.sleep)
def increaseX(x):
    x += 1
    print('X increased to ' + str(x))
    schedTimer.enter(2, 1, increaseX, (x,))
schedTimer.enter(2, 1, increaseX, (3,))
schedTimer.run()
© www.soinside.com 2019 - 2024. All rights reserved.