我想了解如何blocking在方法中使用可选参数scheduler.run(blocking=True)。任何实际的/实际的示例都将非常有帮助。
根据我到目前为止所做的研究,blocking可选参数的意图是用于非阻塞或异步应用程序[1] [2]。以下是schduler的主要运行循环(来自python 3.6库sched.py)。在执行该代码之后,我注意到,只要blocking将设置为False,就立即返回目标时间与当前时间之间的时差,除非目标时间过去了,在这种情况下将执行该操作。
while True:
with lock:
if not q:
break
time, priority, action, argument, kwargs = q[0]
now = timefunc()
if time > now:
delay = True
else:
delay = False
pop(q)
if delay:
if not blocking:
return time - now
delayfunc(time - now)
else:
action(*argument, **kwargs)
delayfunc(0) # Let other threads run
Run Code Online (Sandbox Code Playgroud)
在我看来,非阻塞设计要求我继续运行调度程序,直到队列干净为止。因此,我正在考虑自己维护任务队列,并继续将scheduler.run任务推入队列(如下面的代码。)这是否是理想的设计?使用非阻塞调度程序的正确方法是什么?
def action():
print('action at: ', datetime.now())
if __name__ …Run Code Online (Sandbox Code Playgroud)