Python调度程序在默认值blocking=True
下工作良好:
import sched, time
def print_time(a):
print("From print_time", time.time(), a)
s = sched.scheduler()
s.enter(2, priority=1, action=print_time, argument=(1,))
s.enter(2, priority=2, action=print_time, argument=(2,))
print(time.time())
s.run() # blocks until the last scheduled task is finished
print('finished')
但是我无法使其与blocking=False
一起使用。
实际上是:
s.run(block=False)
print('finished')
该脚本会立即停止,因此显然无法正常运行(这是正常行为)。但是:
s.run(block=False) time.sleep(10) # sleeping to make sure the script does not terminate immediately, print('finished') # but in real situation, there would be other tasks here
也不起作用:奇怪的是任务没有执行。
我不太了解文档:
如果阻塞为false,则由于到期时间最快(如果有的话)执行已调度的事件,然后在调度程序中返回下一个已调度的呼叫的截止日期(如果有的话。
如何在
blocking=False
模式下使用调度程序?
Python调度程序在默认值blocking = True下工作良好,正确:导入调度,时间定义print_time(a):print(“ From print_time”,time.time(),a)s = sched.scheduler()s.enter( 2,优先级= 1,...
我找到了解决方案:blocking=False
模式允许同时执行其他操作并不时轮询新事件。这有效: