etcd的异步Python客户端
项目描述
Etcd的Python客户端 https://github.com/coreos/etcd
官方文档: http://python-aio-etcd.readthedocs.org/
安装
预先要求
此版本的python-etcd将仅与2.0.x或更高版本的etcd服务器正确工作。如果您正在运行较旧版本的etcd,请使用python-etcd 0.3.3或更早版本。
此客户端已知与Python 3.5兼容。由于使用了“async def”语法,它将无法在较旧版本的Python中运行。
不支持Python 2。
从源代码
$ python setup.py install
使用方法
与之前版本相比,客户端的基本方法已更改,以反映新的API结构;然而,已经维护了一个兼容层,因此您不一定需要重写所有现有的代码。
创建客户端对象
import aio_etcd as etcd
client = etcd.Client() # this will create a client against etcd server running on localhost on port 4001
client = etcd.Client(port=4002)
client = etcd.Client(host='127.0.0.1', port=4003)
client = etcd.Client(host=(('127.0.0.1', 4001), ('127.0.0.1', 4002), ('127.0.0.1', 4003)))
client = etcd.Client(host='127.0.0.1', port=4003, allow_redirect=False) # wont let you run sensitive commands on non-leader machines, default is true
# If you have defined a SRV record for _etcd._tcp.example.com pointing to the clients
client = etcd.Client(srv_domain='example.com', protocol="https")
# create a client against https://api.example.com:443/etcd
client = etcd.Client(host='api.example.com', protocol='https', port=443, version_prefix='/etcd')
写入键
await client.write('/nodes/n1', 1)
# with ttl
await client.set('/nodes/n1', 1)
# Equivalent, for compatibility reasons.
await client.write('/nodes/n2', 2, ttl=4)
# sets the ttl to 4 seconds
读取键
(await client.read('/nodes/n2')).value
# read a value
(await client.get('/nodes/n2')).value
# Equivalent, for compatibility reasons.
await client.read('/nodes', recursive = True)
# get all the values of a directory, recursively.
# raises etcd.EtcdKeyNotFound when key not found
try:
client.read('/invalid/path')
except etcd.EtcdKeyNotFound:
# do something
print "error"
删除键
await client.delete('/nodes/n1')
原子比较和交换
await client.write('/nodes/n2', 2, prevValue = 4)
# will set /nodes/n2 's value to 2 only if its previous value was 4
await client.write('/nodes/n2', 2, prevExist = False)
# will set /nodes/n2 's value to 2 only if the key did not exist before
await client.write('/nodes/n2', 2, prevIndex = 30)
# will set /nodes/n2 's value to 2 only if the key was last modified at index 30
await client.test_and_set('/nodes/n2', 2, 4)
#equivalent to client.write('/nodes/n2', 2, prevValue = 4)
您还可以原子地更新结果
await client.write('/foo','bar')
result = await client.read('/foo')
print(result.value) # bar
result.value += u'bar'
updated = await client.update(result)
# if any other client wrote to '/foo' in the meantime this will fail
print(updated.value) # barbar
监视键
result = await client.read('/nodes/n1')
# start from a known initial value
result = await client.read('/nodes/n1', wait = True, waitIndex = result.modifiedIndex+1)
# will wait till the key is changed, and return once it's changed
result = await client.read('/nodes/n1', wait = True, waitIndex = 10)
# get all changes on this key starting from index 10
result = await client.watch('/nodes/n1')
# equivalent to client.read('/nodes/n1', wait = True)
result = await client.watch('/nodes/n1', index = result.modifiedIndex+1)
如果您想超时read()调用,请将其包装在asyncio.wait_for中
result = await asyncio.wait_for(client.read('/nodes/n1', wait=True), timeout=30)
刷新键TTL
(从etcd 2.3.0开始) etcd中的键可以在不通知当前监视器的情况下刷新。
这可以通过在更新TTL时将刷新设置为true来实现。
在刷新时,您无法更新键的值。
client.write('/nodes/n1', 'value', ttl=30) # sets the ttl to 30 seconds
client.refresh('/nodes/n1', ttl=600) # refresh ttl to 600 seconds, without notifying current watchers
锁定模块
# Initialize the lock object:
# NOTE: this does not acquire a lock
from aio_etcd.lock import Lock
client = etcd.Client()
# Or you can custom lock prefix, default is '/_locks/' if you are using HEAD
client = etcd.Client(lock_prefix='/my_etcd_root/_locks')
lock = etcd.Lock(client, 'my_lock_name')
# Use the lock object:
await lock.acquire(blocking=True, # will block until the lock is acquired
lock_ttl=None) # lock will live until we release it
lock.is_acquired # True
await lock.acquire(lock_ttl=60) # renew a lock
await lock.release() # release an existing lock
lock.is_acquired # False
# The lock object may also be used as a context manager:
async with Lock(client, 'customer1') as my_lock:
do_stuff()
my_lock.is_acquired # True
await my_lock.acquire(lock_ttl=60)
my_lock.is_acquired # False
获取集群中的机器
machines = await client.machines()
获取集群的领导者
leaderinfo = await client.leader()
在目录中生成一个顺序键
x = await client.write("/dir/name", "value", append=True)
print("generated key: " + x.key)
# actually the whole path
print("stored value: " + x.value)
列出目录内容
#stick a couple values in the directory
await client.write("/dir/name", "value1", append=True)
await client.write("/dir/name", "value2", append=True)
directory = await client.get("/dir/name")
# loop through a directory's children
for result in directory.children:
print(result.key + ": " + result.value)
# or just get the first child value
print(directory.next(children).value)
开发设置
提供了常用的setuptools命令。
$ python3 setup.py install
为了测试,你应该在你的系统路径中有etcd。
$ python3 setup.py test
要生成文档,
$ cd docs
$ make
发布指南
为了发布
在NEWS.txt和setup.py中更新发布日期/版本
运行 'python setup.py sdist'
在dist/中测试生成的源分发版
上传到PyPI: 'python setup.py sdist register upload'
项目详情
关闭
aio_etcd-0.4.6.1.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fb32e36bb1059fc8bd006436cec6f8c246deb1461b1d052cf3ec7929fb18d88f |
|
MD5 | ad6e4731de650237ff81fe533582730b |
|
BLAKE2b-256 | 0c2556c6c5d6ae1a892354eda8ecd7416dbffa731d1526f8daa3b1fd19ba0fc4 |