SherlockPython 分布式锁

联合创作 · 2023-09-29 13:42

Sherlock 是一个易用的 Python 分布式进程内锁机制库,你可选择锁同步的不同后台。

示例代码:

import sherlock
from sherlock import Lock

# Configure Sherlock's locks to use Redis as the backend,
# never expire locks and retry acquiring an acquired lock after an
# interval of 0.1 second.
sherlock.configure(backend=sherlock.backends.REDIS,
                   expire=None,
                   retry_interval=0.1)

# Note: configuring sherlock to use a backend does not limit you
# another backend at the same time. You can import backend specific locks
# like RedisLock, MCLock and EtcdLock and use them just the same way you
# use a generic lock (see below). In fact, the generic Lock provided by
# sherlock is just a proxy that uses these specific locks under the hood.

# acquire a lock called my_lock
lock = Lock('my_lock')

# acquire a blocking lock
lock.acquire()

# check if the lock has been acquired or not
lock.locked() == True

# release the lock
lock.release()
浏览 1
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报