Search code examples
celeryworkercelery-task

(celery) :Run task in a specific worker?


I have many workers from different server in celery,(the below picture shows) enter image description here

and many tasks:

@task(name="task1")
def task1():
   .......

@task(name="task2")
def task2():
   ......

I want to run "task1" only on the "celery@40.218testlab_website" worker. How should I config?


Solution

  • you can give you workers different names, assign each worker to read from a specific queue when running, aka:

    celery -A tasks -n worker1 -q queue1 --loglevel=info
    celery -A tasks -n worker2 -q queue2 --loglevel=info
    ...
    

    and than add router, for example:

    def route_task(name, args, kwargs, options, task=None, **kw):
            if name == 'task1':
                return 'queue1'
            elif name == 'task2':
                return 'queue2'
            return None
    

    Note, the implementation depends on the celery version you run (i'm using 3.1) - it changed a little in 4. There are even simpler router in the link I've added (in configuration) - check this out.

    Good luck