# 可以过期的python线程锁,基于python字典实现的锁可以过期
实现方式类似于redis锁过期的实现机制。使用字典代替 redis服务。
# 安装方式
pip install expire_lock
# 过期锁用法
```python
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) 完全不一样,原生锁
```python
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秒过期完全不一样。
'''
```
Raw data
{
"_id": null,
"home_page": "https://github.com/ydf0509/expire_lock",
"name": "expire-lock",
"maintainer": "ydf",
"docs_url": null,
"requires_python": "",
"maintainer_email": "ydf0509@sohu.com",
"keywords": "lock,lock timeout,lock expire,expire",
"author": "bfzs",
"author_email": "ydf0509@sohu.com",
"download_url": "https://files.pythonhosted.org/packages/8e/e1/45a317c53153f658707f2bf51897769b40045f67161c2f72f8970a63b3d3/expire_lock-0.3.tar.gz",
"platform": "all",
"description": "\r\n\r\n# \u53ef\u4ee5\u8fc7\u671f\u7684python\u7ebf\u7a0b\u9501\uff0c\u57fa\u4e8epython\u5b57\u5178\u5b9e\u73b0\u7684\u9501\u53ef\u4ee5\u8fc7\u671f\r\n\r\n\u5b9e\u73b0\u65b9\u5f0f\u7c7b\u4f3c\u4e8eredis\u9501\u8fc7\u671f\u7684\u5b9e\u73b0\u673a\u5236\u3002\u4f7f\u7528\u5b57\u5178\u4ee3\u66ff redis\u670d\u52a1\u3002\r\n\r\n# \u5b89\u88c5\u65b9\u5f0f \r\npip install expire_lock\r\n\r\n# \u8fc7\u671f\u9501\u7528\u6cd5\r\n```python\r\nimport time\r\nfrom threading import Thread\r\nfrom expire_lock import ExpireLockConf, ExpireLockContextManager\r\n\r\nlockx1_expire = ExpireLockConf(expire_seconds=4, lock_key='test_lock_name_expire', )\r\n\r\n\r\ndef f(x):\r\n with ExpireLockContextManager(lockx1_expire):\r\n print(x, time.time())\r\n time.sleep(5)\r\n\r\n\r\n\r\nfor i in range(100):\r\n Thread(target=f, args=[i]).start()\r\n\r\n''''\r\n400\u79d2\u949f\u5185\u5c31\u628a100\u4e2a\u51fd\u6570\u7684print(x)\u8fd0\u884c\u5b8c\u6210\u4e86\uff0c \u8fc7\u671f\u9501\u7684\u8bbe\u7f6e expire=4 \u548c\u539f\u751f\u9501.acquire(timeout=4) \u4f5c\u7528\u5b8c\u5168\u4e0d\u540c\u3002\r\n\r\n\u8fc7\u671f\u9501\u610f\u601d\u662f\u4e00\u4e2a\u9501\u83b7\u53d6\u540e\uff0c\u6700\u591a\u80fd\u5360\u7528\u8fd9\u4e2a\u9501n\u79d2\u3002\r\n\u539f\u751f\u9501.acquire(timeout=4) \u610f\u601d\u662f\u6700\u591a\u53ea\u7b49\u5f85\u8fd9\u4e2a\u95014\u79d2\u949f\uff0c\u5f3a\u884c\u83b7\u5f97\u9501\u3002\r\n'''\r\n```\r\n\r\n\u548c\u539f\u751f\u7ebf\u7a0b\u9501\u7684 lock.acquire(timeout=xx) \u5b8c\u5168\u4e0d\u4e00\u6837\uff0c\u539f\u751f\u9501\r\n\r\n```python\r\n\r\nimport time\r\nfrom threading import Thread, Lock\r\n\r\ntest_raw_lock = Lock()\r\n\r\n\r\ndef test_raw_lock_fun(x):\r\n try:\r\n test_raw_lock.acquire(timeout=4)\r\n print(x, time.time())\r\n time.sleep(5)\r\n test_raw_lock.release()\r\n except Exception as e:\r\n if 'release unlocked lock' in str(e):\r\n return\r\n print(e)\r\n\r\n\r\nfor i in range(100):\r\n Thread(target=test_raw_lock_fun, args=[i]).start()\r\n\r\n\r\n''''\r\n4\u79d2\u949f\u5185\u5c31\u628a100\u4e2a\u51fd\u6570\u7684print(x)\u8fd0\u884c\u5b8c\u6210\u4e86\uff0c \u539f\u751f\u9501.acquire(timeout=4) timeout \u548c \u8fc7\u671f\u9501\u76844\u79d2\u8fc7\u671f\u5b8c\u5168\u4e0d\u4e00\u6837\u3002\r\n'''\r\n```\r\n",
"bugtrack_url": null,
"license": "BSD License",
"summary": "\u53ef\u4ee5\u8fc7\u671f\u7684python\u7ebf\u7a0b\u9501\uff0c\u57fa\u4e8epython\u5b57\u5178\u5b9e\u73b0\u7684\u9501\u53ef\u4ee5\u8fc7\u671f,\u5b9e\u73b0\u65b9\u5f0f\u7c7b\u4f3c\u4e8eredis\u9501\u8fc7\u671f\u7684\u5b9e\u73b0\u673a\u5236\u3002\u4f7f\u7528\u5b57\u5178\u4ee3\u66ff redis\u670d\u52a1\u3002",
"version": "0.3",
"project_urls": {
"Homepage": "https://github.com/ydf0509/expire_lock"
},
"split_keywords": [
"lock",
"lock timeout",
"lock expire",
"expire"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8ee145a317c53153f658707f2bf51897769b40045f67161c2f72f8970a63b3d3",
"md5": "1e723bb363a86dfe67e5ad182c21d08e",
"sha256": "0e11118b964eb89178fa6debf721a54073adee8aa5b55d00bd15df591eb8e2b2"
},
"downloads": -1,
"filename": "expire_lock-0.3.tar.gz",
"has_sig": false,
"md5_digest": "1e723bb363a86dfe67e5ad182c21d08e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3618,
"upload_time": "2023-06-22T12:39:31",
"upload_time_iso_8601": "2023-06-22T12:39:31.128448Z",
"url": "https://files.pythonhosted.org/packages/8e/e1/45a317c53153f658707f2bf51897769b40045f67161c2f72f8970a63b3d3/expire_lock-0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-22 12:39:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ydf0509",
"github_project": "expire_lock",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "expire-lock"
}