可以过期的python线程锁,基于python字典实现的锁可以过期,实现方式类似于redis锁过期的实现机制。使用字典代替 redis服务。
Project description
可以过期的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秒过期完全不一样。
'''
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
expire_lock-0.3.tar.gz
(3.6 kB
view details)
File details
Details for the file expire_lock-0.3.tar.gz
.
File metadata
- Download URL: expire_lock-0.3.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e11118b964eb89178fa6debf721a54073adee8aa5b55d00bd15df591eb8e2b2 |
|
MD5 | 1e723bb363a86dfe67e5ad182c21d08e |
|
BLAKE2b-256 | 8ee145a317c53153f658707f2bf51897769b40045f67161c2f72f8970a63b3d3 |