如何覆盖芹菜任务的后端 [英] How do I override the backend for celery tasks

查看:71
本文介绍了如何覆盖芹菜任务的后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将Redis用作结果后端.但是,对于一项任务,我们想重写它以使用RabbitMQ代替.

we're using Redis as our result backend. However for one task we'd like to override this to use RabbitMQ instead.

Task.backend 的文档说:

用于此任务的结果存储后端.默认为CELERY_RESULT_BACKEND设置

The result store backend to use for this task. Defaults to the CELERY_RESULT_BACKEND setting

所以我假设我们可以将 Task.backend 设置为 CELERY_RESULT_BACKEND 可接受的相同格式的字符串.

So I had assumed that we could set Task.backend to a string of the same format accepted by CELERY_RESULT_BACKEND.

所以我试试这个:

celeryconfig.py

CELERY_RESULT_BACKEND = "redis://redis-host:7777"

tasks.py

@app.task(backend='amqp://guest@localhost/tasks-stg')
def my_task(params):
    ...

但是工人失败了:

[2015-05-07 13:33:49,264: ERROR/Worker-1] Process Worker-1
Traceback (most recent call last):
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/process.py", line 292, in _bootstrap
    self.run()
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/pool.py", line 286, in run
    self.after_fork()
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/pool.py", line 389, in after_fork
    self.initializer(*self.initargs)
  File "/project/python2.7_x64/lib/python2.7/site-packages/celery/concurrency/prefork.py", line 81, in process_initializer
    app=app)
  File "/project/python2.7_x64/lib/python2.7/site-packages/celery/app/trace.py", line 178, in build_tracer
    store_result = backend.store_result
AttributeError: 'str' object has no attribute 'store_result'

推荐答案

文档不正确. Task.backend 实际上是 celery.backends 中的一个后端类的实例.在这种情况下,要覆盖任务类,我必须这样做:

The documentation is incorrect. Task.backend is actually an instance of a backend class from celery.backends. In this case to override the task class I had to do this:

from celery.backends.amqp import AMQPBackend

@app.task(backend=AMQPBackend(app, url='amqp://guest@localhost/tasks-stg'))
def my_task(params):
    ...

但是,工作人员继续使用默认类,并且似乎没有提供重写此类的方法.

However the workers continue to use the default class and don't seem to offer a way to override this.

这篇关于如何覆盖芹菜任务的后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆