Skip to main content

python etcd network mutux lock,support watch ,force ,incr lock time

Project description

#pyetcdlock

基于etcd的分布式锁,简单说就是利用etcd.test_and_set函数来判断lock key是否被占用,存在那就说明有人占用。在创建key的时候加入了ttl,防止因为进程异常退出而没有释放锁。

更新日志:

加入watch机制,更快的解决了etcd客户端因为异常退出后,key需要等待ttl过期的问题。简单说,模拟了zookeeper那种临时节点的概念。


**曾经写过关于分布式互斥锁的文章:**

zookeeper:
[http://xiaorui.cc/2015/04/09/python-zookeeper%E8%A7%A3%E5%86%B3redis%E5%81%9A%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%B8%A6%E6%9D%A5%E7%9A%84%E5%9D%91/](http://xiaorui.cc/2015/04/09/python-zookeeper%E8%A7%A3%E5%86%B3redis%E5%81%9A%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%B8%A6%E6%9D%A5%E7%9A%84%E5%9D%91/)

redis:
[http://xiaorui.cc/2014/12/19/python%E4%BD%BF%E7%94%A8redis%E5%AE%9E%E7%8E%B0%E5%8D%8F%E5%90%8C%E6%8E%A7%E5%88%B6%E7%9A%84%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/](http://xiaorui.cc/2014/12/19/python%E4%BD%BF%E7%94%A8redis%E5%AE%9E%E7%8E%B0%E5%8D%8F%E5%90%8C%E6%8E%A7%E5%88%B6%E7%9A%84%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/)

### 安装方法

```
git clone https://github.com/rfyiamcool/pyetcdlock.git
cd pyetcdlock
python setup.py
```

pypi的安装方式 (话说,pypi有些问题,你在pypi search搜东西的时候,不显示详细信息)

```
pip instlal pyetcdlock
```

模块说明:

ttl : expire过期时间,这样可以指定锁定的时间

renew : 如果你的ttl时间将要过期了,那么可以使用renew(),再加点锁定的ttl时间

force_acquire : 强制获取锁


### 创建连接

```
import etcd
from pyetcdnetlock import Lock

client = client = etcd.Client(host='127.0.0.1')
lock = Lock(client, 'path/to/my/key', ttl=30, renewSecondsPrior=5)
```

### 使用方法

```
if lock.acquire(timeout=20):
# some work
lock.renew()
# some other work
lock.release()
else
# failed to acquire the lock in 20 seconds
pass

```

OR

```
with lock as l:
#如果时间不够,可以用renew()参数,加点时间
l.renew()
# some other work
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyetcdlock-1.2.0.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file pyetcdlock-1.2.0.tar.gz.

File metadata

  • Download URL: pyetcdlock-1.2.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyetcdlock-1.2.0.tar.gz
Algorithm Hash digest
SHA256 88dbe7bdece0a59a4e7b0d66ee59ff78bc0a339a4df60b311900521ef5fb1c5d
MD5 6412e9d3d419fb36336441a28424277b
BLAKE2b-256 12adf2b770413da933f4859c13c482ff244186115fc396a41f49e59076e507ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page