用于etcd的异步python客户机
aio_etcd的Python项目详细描述
etcd的python客户端https://github.com/coreos/etcd
官方文件:http://python-aio-etcd.readthedocs.org/
安装
先决条件
此版本的python etcd只能与etcd服务器版本2.0.x或更高版本一起正常工作。如果您运行的是较旧版本的etcd,请使用python etcd 0.3.3或更早版本。
众所周知,这个客户机使用Python3.5由于使用了“async def”语法,它在旧版本的python中不起作用。
不支持Python2。
来源
$ python setup.py install
用法
客户端的基本方法与以前版本相比有所改变,以反映新的API结构;然而,保持了一个兼容层,这样您就不必重写所有现有代码。
创建客户端对象
importaio_etcdasetcdclient=etcd.Client()# this will create a client against etcd server running on localhost on port 4001client=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 clientsclient=etcd.Client(srv_domain='example.com',protocol="https")# create a client against https://api.example.com:443/etcdclient=etcd.Client(host='api.example.com',protocol='https',port=443,version_prefix='/etcd')
写一个键
awaitclient.write('/nodes/n1',1)# with ttlawaitclient.set('/nodes/n1',1)# Equivalent, for compatibility reasons.awaitclient.write('/nodes/n2',2,ttl=4)# sets the ttl to 4 seconds
读取键
(awaitclient.read('/nodes/n2')).value# read a value(awaitclient.get('/nodes/n2')).value# Equivalent, for compatibility reasons.awaitclient.read('/nodes',recursive=True)# get all the values of a directory, recursively.# raises etcd.EtcdKeyNotFound when key not foundtry:client.read('/invalid/path')exceptetcd.EtcdKeyNotFound:# do somethingprint"error"
删除键
awaitclient.delete('/nodes/n1')
原子比较和交换
awaitclient.write('/nodes/n2',2,prevValue=4)# will set /nodes/n2 's value to 2 only if its previous value was 4awaitclient.write('/nodes/n2',2,prevExist=False)# will set /nodes/n2 's value to 2 only if the key did not exist beforeawaitclient.write('/nodes/n2',2,prevIndex=30)# will set /nodes/n2 's value to 2 only if the key was last modified at index 30awaitclient.test_and_set('/nodes/n2',2,4)#equivalent to client.write('/nodes/n2', 2, prevValue = 4)
您还可以原子更新结果:
awaitclient.write('/foo','bar')result=awaitclient.read('/foo')print(result.value)# barresult.value+=u'bar'updated=awaitclient.update(result)# if any other client wrote to '/foo' in the meantime this will failprint(updated.value)# barbar
看一把钥匙
result=awaitclient.read('/nodes/n1')# start from a known initial valueresult=awaitclient.read('/nodes/n1',wait=True,waitIndex=result.modifiedIndex+1)# will wait till the key is changed, and return once it's changedresult=awaitclient.read('/nodes/n1',wait=True,waitIndex=10)# get all changes on this key starting from index 10result=awaitclient.watch('/nodes/n1')# equivalent to client.read('/nodes/n1', wait = True)result=awaitclient.watch('/nodes/n1',index=result.modifiedIndex+1)
如果要超时read()调用,请将其包装在异步中。等待:
result=awaitasyncio.wait_for(client.read('/nodes/n1',wait=True),timeout=30)
刷新按键ttl
(因为etcd 2.3.0)可以在不通知当前观察者的情况下刷新etcd中的键
这可以通过在更新ttl时将refresh设置为true来实现。
刷新密钥时不能更新其值
client.write('/nodes/n1','value',ttl=30)# sets the ttl to 30 secondsclient.refresh('/nodes/n1',ttl=600)# refresh ttl to 600 seconds, without notifying current watchers
锁定模块
# Initialize the lock object:# NOTE: this does not acquire a lockfromaio_etcd.lockimportLockclient=etcd.Client()# Or you can custom lock prefix, default is '/_locks/' if you are using HEADclient=etcd.Client(lock_prefix='/my_etcd_root/_locks')lock=etcd.Lock(client,'my_lock_name')# Use the lock object:awaitlock.acquire(blocking=True,# will block until the lock is acquiredlock_ttl=None)# lock will live until we release itlock.is_acquired# Trueawaitlock.acquire(lock_ttl=60)# renew a lockawaitlock.release()# release an existing locklock.is_acquired# False# The lock object may also be used as a context manager:asyncwithLock(client,'customer1')asmy_lock:do_stuff()my_lock.is_acquired# Trueawaitmy_lock.acquire(lock_ttl=60)my_lock.is_acquired# False
获取群集中的计算机
machines=awaitclient.machines()
获得集群的领导者
leaderinfo=awaitclient.leader()
在目录中生成顺序键
x=awaitclient.write("/dir/name","value",append=True)print("generated key: "+x.key)# actually the whole pathprint("stored value: "+x.value)
列出目录的内容
#stick a couple values in the directoryawaitclient.write("/dir/name","value1",append=True)awaitclient.write("/dir/name","value2",append=True)directory=awaitclient.get("/dir/name")# loop through a directory's childrenforresultindirectory.children:print(result.key+": "+result.value)# or just get the first child valueprint(directory.next(children).value)
开发设置
提供常用的setuptools命令。
$ python3 setup.py install
要进行测试,您的系统路径中应该有etcd:
$ python3 setup.py test
要生成文档,
$ cd docs
$ make
发布howto
发布
- Update release date/version in NEWS.txt and setup.py
- Run ‘python setup.py sdist’
- Test the generated source distribution in dist/
- Upload to PyPI: ‘python setup.py sdist register upload’