跳转到主要内容

etcd的异步Python客户端

项目描述

Etcd的Python客户端 https://github.com/coreos/etcd

官方文档: http://python-aio-etcd.readthedocs.org/

https://travis-ci.org/M-o-a-T/python-aio-etcd.png?branch=master https://coveralls.io/repos/M-o-a-T/python-aio-etcd/badge.svg?branch=master&service=github

安装

预先要求

此版本的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

发布指南

为了发布

  1. 在NEWS.txt和setup.py中更新发布日期/版本

  2. 运行 'python setup.py sdist'

  3. 在dist/中测试生成的源分发版

  4. 上传到PyPI: 'python setup.py sdist register upload'

项目详情


下载文件

下载适合您平台的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。

源分发

aio_etcd-0.4.6.1.tar.gz (19.3 kB 查看哈希)

上传时间

由以下提供支持