简体   繁体   中英

How to get next celery task_id or how to get task id from task itself (Flask) or how to set custom id for task?

I have some celery in my flask app:

# Initialize Celery
celery = Celery("DockerApp", broker=config['CELERY_BROKER_URL'])
celery.conf.update(config)

And I have task:

@celery.task
def some_task(data):
    some_task.request.id #not working
    #my task code goes here

I need id of that task within task itself or I need to set that id my self. Can somebody help?

When I call task:

result = some_task.delay(data)
print result.id #this is correct

That gives me correct result.

You can also access the task's request inside the task with:

@celery.task(bind=True)
def some_task(self, data):
    print self.request.id

I got this with:

from celery import current_task #in task definition
print current_task.request.id

Thanks anyway.

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