简体   繁体   中英

celery: how to rename a task and route it to a queue

Could you please advise on how to assign a custom name on a celery task ? By default, it gets the module name, but I was wondering in case I would like to send a task to a specific queue, how I can achieve that ?

In other words,

given that the related Celery config is like:

CELERY_QUEUES = (
    Queue('celery.A', Exchange("xxx"), routing_key='celery.A'),
    Queue('celery.B', Exchange("xxx"), routing_key='celery.B'),
)
CELERY_ROUTES = {
    'A': {'queue': 'celery.A', 'routing_key': 'celery.A'},
    'B': {'queue': 'celery.B', 'routing_key': 'celery.B'},
}

and there is a function:

@app.task(ignore_result=True)
def xxx(netelement):
    pass

What I would like to achieve is: if netelement == A do task.name=A (and then, according to the mapping, route it to queue A)

Is that possible ?

You can't change the routing in the function body because by definition the task is already executing and has already been routed.

The one obvious solution I can think of is to have your first task delegate to another (asynchronously so it can get routed appropriately).

@app.task(ignore_result=True)
def xxx(netelement):
    if netelement == A:
       task_A.delay()
    else:
       task_B.delay()

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