Python 3.x,Celery 4.x ...
我有一个基于班级的任务。
myproj/celery.py
from celery import Celery
# django settings stuff...
app = Celery('myproj')
app.autodiscover_tasks()
app1/tasks.py
import celery
class EmailTask(celery.Task):
def run(self, *args, **kwargs):
self.do_something()
如果我这样做:
$ celery worker -A myproj -l info
[tasks]
. app2.tasks.debug_task
. app2.tasks.test
因此,芹菜装饰器用于注册任务,但基于类的任务未注册。
更新1:
如果我将以下行添加到app1/tasks.py
from myproj.celery import app
email_task = app.tasks[EmailTask.name]
$ celery worker -A myproj -l info
File "myproj/app1/tasks.py", line 405, in <module>
email_task = app.tasks[EmailTask.name]
File "/usr/local/lib/python3.5/site-packages/celery/app/registry.py", line 19, in __missing__
raise self.NotRegistered(key)
celery.exceptions.NotRegistered
更新2:
我可以通过包装器同步执行我的任务(run
)。但是,我无法运行任务异步,即通过delay
。
app1/tasks.py
@app.task
def email_task():
"""
Wrapper to call class based task
"""
task = EmailTask()
# task.delay() # Won't work!!!
task.run()
$./manage.py shell
> from app1.tasks import EmailTask
> task1 = EmailTask()
> task1.run() # a-okay
> task2 = EmailTask()
> task2.delay() # nope
<AsyncResult: 1c03bad9-169a-4a4e-a56f-7d83892e8bbc>
# And on the worker...
[2017-01-22 08:07:28,120: INFO/PoolWorker-1] Task app1.tasks.email_task[41e5bc7d-058a-400e-9f73-c853c0f60a2a] succeeded in 0.0701281649817247s: None
[2017-01-22 08:10:31,909: ERROR/MainProcess] Received unregistered task of type None.
The message has been ignored and discarded.
答案 0 :(得分:10)
你可以找到完整的描述here,但对我来说,添加
就足够了from myapp.celery import app
app.tasks.register(MyTaskTask())
答案 1 :(得分:4)
使用celery==4.2.1
时,我必须使用返回值Celery.register_task()
作为任务实例来调用delay()
:
# my_app/tasks.py
import celery
from my_app.celery import app
class MyTask(celery.Task):
def run(self):
[...]
MyTask = app.register_task(MyTask())
然后使用它:
# my_app/app.py
from my_app.tasks import MyTask
[...]
MyTask.delay()
该解决方案在Github issue中进行了描述,其名称为documented here。
Hth, dtk