Browse Source

Update task-cookbook lock code example (#4344)

Add checking did current worker/thread acquire the lock before releasing it, to avoid the situation when one worker release lock owned by someone else
Mikołaj 7 years ago
parent
commit
e5cf4ba25f
1 changed files with 3 additions and 2 deletions
  1. 3 2
      docs/tutorials/task-cookbook.rst

+ 3 - 2
docs/tutorials/task-cookbook.rst

@@ -59,10 +59,11 @@ For this reason your tasks run-time shouldn't exceed the timeout.
         finally:
             # memcache delete is very slow, but we have to use it to take
             # advantage of using add() for atomic locking
-            if monotonic() < timeout_at:
+            if monotonic() < timeout_at and status:
                 # don't release the lock if we exceeded the timeout
                 # to lessen the chance of releasing an expired lock
-                # owned by someone else.
+                # owned by someone else
+                # also don't release the lock if we didn't acquire it
                 cache.delete(lock_id)
 
     @task(bind=True)