简体   繁体   中英

How to execute celery task when new message arrives to queue?

I have a node app that puts messages in a queue using RabbitMQ. Then i have some workers in python using celery. I want celery to automatically execute some task when a new message is posted to that queue. How can i achieve this? Any help is appreciated.

Try this: In node your message should have this format

var message = {
             "id": "4cc7438e-afd4-4f8f-a2f3-f46567e7ca77",
             "task": "task_name",
             "args": ["this is my arg"],
             "kwargs": {},
             "retries": 0
            }

And in Celery your task should be defined like this:

@app.task(serializer='json', name='task_name')
def task1(arg1):
    print arg1

Also don't forget to configure routes in your celery config file, for example:

app.conf.update(
  CELERY_TASK_RESULT_EXPIRES=3600,
  CELERY_ROUTES = {'tasks.task1': {'queue': 'queue_name'}},
  CELERY_ACCEPT_CONTENT = ['application/json']
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM