我有两种工作:我要串行运行的工作和我要并行并行运行的工作。但是,我希望并行作业以串行方式安排(如果您仍在关注)。那就是:
我认为它有2个redis队列,一个只有一个工作线程的serial_queue。还有一个其中有多个工作线程的parallel_queue。
serial_queue.schedule(
scheduled_time=datetime.utcnow(),
func=job_a,
...)
serial_queue.schedule(
scheduled_time=datetime.utcnow(),
func=job_b,
...)
def parallel_c():
for task in range(args.n_tasks):
queue_concurrent.schedule(
scheduled_time=datetime.utcnow(),
func=job_c,
...)
serial_queue.schedule(
scheduled_time=datetime.utcnow(),
func=parallel_c,
...)
但是此设置当前存在以下错误:
AttributeError: module '__main__' has no attribute 'schedule_fetch_tweets'
。如何为python-rq
正确打包此功能?
答案 0 :(得分:2)
该解决方案需要一些技巧,因为您必须导入当前脚本,就像它是一个外部模块一样。
例如。 schedule_twitter_jobs.py
的内容为:
from redis import Redis
from rq_scheduler import Scheduler
import schedule_twitter_jobs
# we are importing the very module we are executing
def schedule_fetch_tweets(args, queue_name):
''' This is the child process to schedule'''
concurrent_queue = Scheduler(queue_name=queue_name+'_concurrent', connection=Redis())
# this scheduler is created based on a queue_name that will be passed in
for task in range(args.n_tasks):
scheduler_concurrent.schedule(
scheduled_time=datetime.utcnow(),
func=app.controller.fetch_twitter_tweets,
args=[args.statuses_backfill, fill_start_time])
serial_queue = Scheduler(queue_name='myqueue', connection=Redis())
serial_queue.schedule(
'''This is the first schedule.'''
scheduled_time=datetime.utcnow(),
func=schedule_twitter_jobs.schedule_fetch_tweets,
#now we have a fully-qualified reference to the function we need to schedule.
args=(args, ttl, timeout, queue_name)
#pass through the args to the child schedule
)