ashchristopher
ashchristopher

Reputation: 26281

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

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()

Upvotes: 6

Views: 5349

Answers (1)

ashchristopher
ashchristopher

Reputation: 26281

From askol on IRC:

MyTask.apply_async(args=[], kwargs={}, queue='high_priority_queue')

Upvotes: 10

Related Questions