Register Celery Class-based Task

后端 未结 2 728
慢半拍i
慢半拍i 2020-12-31 04:02

Python 3.x, Celery 4.x...

I have a class-based task.

myproj/celery.py

from celery import Celery

# django settings stuff...

app         


        
相关标签:
2条回答
  • 2020-12-31 04:34

    You can find full description here, but for me it was enough to add

    from myapp.celery import app
    app.tasks.register(MyTaskTask())
    
    0 讨论(0)
  • 2020-12-31 04:57

    With celery==4.2.1 I had to use the return value of Celery.register_task() as the task instance to call delay() on:

    # my_app/tasks.py
    import celery
    
    from my_app.celery import app
    
    class MyTask(celery.Task):
        def run(self):
            [...]
    
    MyTask = app.register_task(MyTask())
    

    Then to use it:

    # my_app/app.py
    from my_app.tasks import MyTask
    
    [...]
    
    MyTask.delay()
    

    The solution was described in a Github issue and is documented here.

    Hth, dtk

    0 讨论(0)
提交回复
热议问题