跳转到主要内容

使用数据库简单实现Django应用的锁定系统。

项目描述

Privex的Django数据库锁定管理器

Build Status Codecov PyPi Version License Button PyPI - Downloads PyPI - Python Version PyPI - Django Version GitHub last commit

+===================================================+
|                 © 2019 Privex Inc.                |
|               https://www.privex.io               |
+===================================================+
|                                                   |
|        Django Database Lock Manager               |
|        License: X11/MIT                           |
|                                                   |
|        Core Developer(s):                         |
|                                                   |
|          (+)  Chris (@someguy123) [Privex]        |
|                                                   |
+===================================================+

Django Database Lock Manager - Easy to use lock system using your Django app's database
Copyright (c) 2019    Privex Inc. ( https://www.privex.io )

使用pip安装

我们建议至少使用Python 3.6 - 我们不能保证与旧版本的兼容性。

pip3 install django-lockmgr

lockmgr添加到您的INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin.apps.SimpleAdminConfig',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    # ...
    'lockmgr'
]   

运行迁移

./manage.py migrate lockmgr

用法

在代码中使用LockMgr如下

from lockmgr.lockmgr import LockMgr, Locked

try:
    with LockMgr('mylock') as lck:
        print('The lock "mylock" is now locked.')
        lck.lock('otherlock')
        print('The lock "otherlock" should also be locked.')
    print('The locks "mylock" and "otherlock" should now both be cleared.')
except Locked as e:
    print('Error! mylock is already locked: ', type(e), str(e))

如果您想等待锁释放,而不是立即抛出异常

from lockmgr.lockmgr import LockMgr, Locked

try:
    # expires=60 means the lock will expire after 60 seconds if you don't renew it.
    # wait=90 (must be in 5 second intervals) means: if the key is locked, retry every 5 seconds, if 90 seconds have 
    #         passed and it's still locked, then give up and raise Locked.
    with LockMgr('somelock', expires=60, wait=90) as lck:
        print('The lock "somelock" is now locked.')
    print('The lock "somelock" should now be cleared.')
except Locked as e:
    print('Error! After retrying for 90 seconds, "somelock" is still locked: ', type(e), str(e))

单元测试

要运行单元测试,克隆项目并创建一个包含数据库详情的.env文件

DB_BACKEND=mysql
DB_NAME=lockmgr
DB_USER=someuser
DB_PASS=mypassword

安装所有必需的依赖项

pip3 install -r requirements.txt -U

现在运行测试(使用--verbose以获得更详细的测试输出)

./manage.py test --verbose

项目详情


下载文件

下载适用于您的平台文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源分布

django-lockmgr-2.0.0.tar.gz (20.1 kB 查看哈希值)

上传时间:

构建分布

django_lockmgr-2.0.0-py3-none-any.whl (24.4 kB 查看哈希值)

上传于 Python 3

支持者

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面