首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用python-rq调度另一个redis作业的redis作业。

用python-rq调度另一个redis作业的redis作业。
EN

Stack Overflow用户
提问于 2018-08-27 19:01:37
回答 1查看 778关注 0票数 2

我有两种类型的作业:一种是连续运行的,另一种是并行运行的。但是,我希望并行作业按顺序安排(如果您还在跟踪的话)。这就是:

  1. Do A.
  2. 等A,做B。
  3. 等待B,并发执行C的2+版本。

我认为它有两个redis队列,一个只有一个工作人员的serial_queue。还有一个parallel_queue,上面有多个工作人员。

代码语言:javascript
复制
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正确地打包此函数

EN

回答 1

Stack Overflow用户

发布于 2018-08-27 22:17:57

解决方案需要一些体操,因为您必须导入当前脚本,就好像它是一个外部模块一样。

举个例子。schedule_twitter_jobs.py的内容如下:

代码语言:javascript
复制
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
   )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52045496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档