简体   繁体   中英

Specify queue for a Task class in Celery

When I create a function task, I can do the following to specify the queue:

@app.task(name='my_task', queue='my_queue')
def some_task():
    return "hey"

Then, I can call this task like this: some_task.delay() and it would send the task to the my_queue .

However, if I have a class that inherits from the celery.Task class, I have to specify the queue every time I call this task:

class MyTask(Task):
    def run(self):
        return "Hey"

MyTask.apply_async(queue="my_queue")

Is there a way to specify this in the class definition? Something like this:

class MyTask(Task):
    queue = 'my_queue'

    def run(self):
         return "Hey"

I cannot find a way to do it in the documentation. Is it possible?

Turns out, that was exactly how you would do it:

class MyTask(Task):
    queue = 'my_queue'

    def run(self):
        return "Hey"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM