I wish a task not to be ran at same time by many users. So I used this code:
REDIS_CLIENT = redis.Redis()
lock = REDIS_CLIENT.lock("task_key", timeout=60)
have_lock = lock.acquire(blocking=True, blocking_timeout=60)
With 2 users, it works well. But with more than 2 users :
- User 1 acquires lock for the task
- User 1 runs task
- User 2 tries acquiring lock for the task but it is already acquired so he waits
- User 3 tries acquiring lock for the task but it is already acquired so he waits
- User 1 task ends
- User 3 acquires lock for the task
- User 3 runs task
- User 2 get angry because User 3 stole his place
At step 6, User 2 or User 3 can acquire lock randomly. But I would like to be sure User 2 acquires it first.
How to set a priority ?
PS : I use Redis to run "unique" tasks with celery : https://stackoverflow.com/a/19499540/5077962