Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define the celery queue property on a Task at runtime?

Tags:

django

celery

Use case:

class MyTask(Task):
    queue = 'default_queue'

    def run(self):
        # do work

Normally I would run the following which would use the 'default_queue' specified.

MyTask.delay()

What I need to do is something like:

if hours_since_last_login > 24:
    MyTask.delay()   # using the queue 'high_priority_queue'
else:
    MyTask.delay()   # using the 'default_queue'

I know I can subclass MyTask to override the queue property, but is there a way to define it at runtime?

Is this the following the correct way to do it?

task = MyTask()
task.queue = 'high_priority_queue'
task.delay()
like image 294
ashchristopher Avatar asked Mar 22 '12 20:03

ashchristopher


People also ask

How do you specify queue in Celery?

The simplest way to do routing is to use the task_create_missing_queues setting (on by default). With this setting on, a named queue that's not already defined in task_queues will be created automatically. This makes it easy to perform simple routing tasks.

What is Celery in message queue?

Celery is an open source asynchronous task queue or job queue which is based on distributed message passing. While it supports scheduling, its focus is on operations in real time. Celery. Stable release. 5.2.3 / December 29, 2021.


1 Answers

From askol on IRC:

MyTask.apply_async(args=[], kwargs={}, queue='high_priority_queue')
like image 146
ashchristopher Avatar answered Sep 27 '22 10:09

ashchristopher