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