[英]python schedule - jobs from python list
I'd like to use python schedule library to schedule a large amount of jobs.我想使用python 调度库来调度大量的作业。
All jobs perform the same operations but on a different input.所有作业都执行相同的操作,但输入不同。
The inputs are stored in a python list.输入存储在 python 列表中。
The idea is to put each input from the list into a shared queue and then process each one of them in sequence.这个想法是将列表中的每个输入放入一个共享队列中,然后按顺序处理它们中的每一个。
Here is an example and my attempt:这是一个例子和我的尝试:
import queue
import time
import threading
import schedule
def job(name):
print(name)
def worker_main():
while 1:
(job_func,job_msg) = jobqueue.get()
job_func(job_msg)
jobqueue.task_done()
name = ['jane', 'alice', 'mary']
jobqueue = queue.Queue()
schedule.every(2).seconds.do(jobqueue.put, (job, name))
worker_thread = threading.Thread(target=worker_main)
worker_thread.start()
while 1:
schedule.run_pending()
time.sleep(1)
Unfortunately, this approach run 1 job for all items in the list all together.不幸的是,这种方法对列表中的所有项目一起运行 1 个作业。 Anyone has any suggestion?
有人有什么建议吗? Thank you.
谢谢你。
Printing every name in each 2 second interval:每 2 秒间隔打印每个名称:
for n in name:
schedule.every(2).seconds.do(jobqueue.put, (job, n))
[Second 2]: jane
[Second 2]: alice
[Second 2]: mary
[Second 4]: jane
[Second 4]: alice
[Second 4]: mary
Printing a single name in each 2 second interval and looping through the list:每 2 秒打印一个名称并循环遍历列表:
from itertools import cycle
name_iter = cycle(name)
schedule.every(2).seconds.do(lambda: jobqueue.put((job, next(name_iter))))
[Second 2]: jane
[Second 4]: alice
[Second 6]: mary
[Second 8]: jane
[Second 10]: alice
[Second 12]: mary
What if I do not want to use cycle but stop when the iterator has been consumed
如果我不想使用循环但在迭代器被消耗后停止怎么办
Looks like you can raise schedule.CancelJob
https://schedule.readthedocs.io/en/stable/examples.html#run-a-job-once看起来你可以提高
schedule.CancelJob
https://schedule.readthedocs.io/en/stable/examples.html#run-a-job-once
name_iter = iter(name)
def queue_name():
try:
jobqueue.put((job, next(name_iter)))
except StopIteration:
return schedule.CancelJob
schedule.every(1).seconds.do(queue_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.