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.
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')
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With