可以过期的python线程锁,基于python字典实现的锁可以过期
实现方式类似于redis锁过期的实现机制。使用字典代替 redis服务。
安装方式
pip install expire_lock
过期锁用法
import time
from threading import Thread
from expire_lock import ExpireLockConf, ExpireLockContextManager
lockx1_expire = ExpireLockConf(expire_seconds=4, lock_key='test_lock_name_expire', )
def f(x):
with ExpireLockContextManager(lockx1_expire):
print(x, time.time())
time.sleep(5)
for i in range(100):
Thread(target=f, args=[i]).start()
''''
400秒钟内就把100个函数的print(x)运行完成了, 过期锁的设置 expire=4 和原生锁.acquire(timeout=4) 作用完全不同。
过期锁意思是一个锁获取后,最多能占用这个锁n秒。
原生锁.acquire(timeout=4) 意思是最多只等待这个锁4秒钟,强行获得锁。
'''
和原生线程锁的 lock.acquire(timeout=xx) 完全不一样,原生锁
import time
from threading import Thread, Lock
test_raw_lock = Lock()
def test_raw_lock_fun(x):
try:
test_raw_lock.acquire(timeout=4)
print(x, time.time())
time.sleep(5)
test_raw_lock.release()
except Exception as e:
if 'release unlocked lock' in str(e):
return
print(e)
for i in range(100):
Thread(target=test_raw_lock_fun, args=[i]).start()
''''
4秒钟内就把100个函数的print(x)运行完成了, 原生锁.acquire(timeout=4) timeout 和 过期锁的4秒过期完全不一样。
'''
Metadata
Release files for expire-lock 0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| expire_lock-0.3.tar.gz | 3.6 kB | Without provenance | Details for expire_lock-0.3.tar.gz |
Release files / expire_lock-0.3.tar.gz
| Download URL | expire_lock-0.3.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e11118b964eb89178fa6debf721a54073adee8aa5b55d00bd15df591eb8e2b2
|
|
BLAKE2b-256 checksum How to use checksums |
8ee145a317c53153f658707f2bf51897769b40045f67161c2f72f8970a63b3d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Security for expire-lock 0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.