Created
July 26, 2023 04:58
-
-
Save anhtran/6b71da7222a817ed83071e4b3da4dca7 to your computer and use it in GitHub Desktop.
Ví dụ cách sử dụng redis Lock để khóa một tác vụ nào đó trong Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from redis.lock import Lock | |
def process_job(): | |
print('worked...') | |
lock_key = f"lock_save:1234" | |
lock = Lock(redis_client, lock_key, timeout=3600) | |
if lock.acquire(blocking=False): | |
try: | |
return process_job() | |
finally: | |
lock.release() | |
else: | |
print('Locked: 1234!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment