简体   繁体   中英

How do I choose a task_id using celery?

For now I get a task_id from the async_result and have to save it the get it back later. Would be better if I knew what the task_id what made of so I can calculate it back instead of pulling from the DB. EG: set a task with task_id=("%s-%s" % (user_id, datetime)) .

You can certainly use "natural ids", but then to be really useful they would have to be reverseable, which doesn't work if you add that timestamp. Also the ids are unique, so two tasks can't have the same id (the behavior then is undefined)

If you have a task to refresh the timeline of a twitter user, then you know that you only want one task running for each user id at any time, so you could use a natural id like:

"update-twitter-timeline-%s" % (user_id)

then always be able to get the result for that task, or revoke the task using that id, no need to manually store it somewhere and look it up.

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