pip install python-etcd
import etcd# 创建etcd连接client = etcd.Client(host='etcd-api.situdata.com', protocol='https', port=443, allow_reconnect=True,)# 向etcd中写数据# prevExist如果数据有的话,就不再插入 client.write('/nodes/n1', 1,ttl=4,prevExist=False)# 读取key对应的valuetry: client.read('/nodes/n1').valueexcept etcd.EtcdKeyNotFound: return "error"# 删除键值对client.delete('/nodes/n1')# 阻塞,直到键被更改,并在其更改后返回,或者在30秒后异常退出client.read('/nodes/n1', wait = True, timeout=30)#全局锁client = etcd.Client()lock = etcd.Lock(client, 'my_lock_name')lock.acquire(blocking=True, # 阻塞,直到锁被获取 lock_ttl=None) # 锁定将一直存在,直到我们释放它lock.is_acquired # True表示存在锁lock.acquire(lock_ttl=60) # 更新一个锁lock.release() # 释放现有的锁lock.is_acquired # False表示不存在锁