Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CELERY_ROUTES - how to route based on task name

Tags:

celery

I'm trying to get celery to route tasks based on the name of the task... basically, I have tasks that are name 'worker.some_name' and 'web.some_name', and I use two different queues, called worker and web respectively. I would like all worker tasks to go to the worker queue and vice-versa. Currently I have a big CELERY_ROUTES dictionary like this:

CELERY_ROUTES = {
    "web.some_name": {
        "queue": "web"
    },
    "web.some_other_name": {
        "queue": "web"
    },
    etc.... }

But I would like something more generic like:

CELERY_ROUTES = (MyRouter(), ) 
class MyRouter(object):
    def route_for_task(self, task, args=None, kwargs=None):
        if task.split('.')[0] == "worker":
            return {"queue": "worker"}
        return {"queue": "web"}

But this doesn't seem to work. Any ideas? Thanks.

like image 767
aclowes Avatar asked Oct 20 '11 18:10

aclowes


2 Answers

You must have used the decorator "@app.task" for the task you've defined in py file.

You can route your task using @app.task(queue='queue_name')

like image 87
Shakil Abbas Avatar answered Sep 29 '22 08:09

Shakil Abbas


You should be able to do what you want by changing the exchange type from direct to topic. This way you can specify the tasks as web.* or worker.*

You can read up on it here: http://ask.github.com/celery/userguide/routing.html#topic-exchanges

like image 27
Dan Goldin Avatar answered Sep 29 '22 09:09

Dan Goldin