Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent the same task to be executed by celery?

I'm implementing a cache server that uses a celery task to update the cache in background. There is only one task that I call it with different arguments (cache keys).

Since after connecting this server to my main production server it will receive tens of requests per second for the same cache key I want to make sure there are never more than one of the update tasks with the same cache key inside celery queue (working as a queue and a set at the same time).

I thought of using a redis set to make sure of that before running the task but I'm looking for a better way.

like image 477
Sina Avatar asked May 04 '16 10:05

Sina


People also ask

How do I stop celery task?

If a task is revoked, the workers ignore the task and do not execute it. If you don't use persistent revokes your task can be executed after worker's restart. revoke has an terminate option which is False by default. If you need to kill the executing task you need to set terminate to True.

How does Celery execute tasks?

Process of Task Execution by Celery can be broken down into:Your application sends the tasks to the task broker, it is then reserved by a worker for execution & finally the result of task execution is stored in the result backend.

Does Celery run tasks in parallel?

Celery task canvas Demonstration of a task which runs a startup task, then parallelizes multiple worker tasks, and then fires-off a reducer task.

What happens when a celery task fails?

Celery will stop retrying after 7 failed attempts and raise an exception.


1 Answers

There is only one way, implement your own lock mechanism. The official doc has a nice example page.. The only limit is your imagination.

Hope this helps.

like image 69
Mauro Rocco Avatar answered Oct 20 '22 16:10

Mauro Rocco