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()