etcd3的异步客户端库
项目描述
txaioetcd是一个由etcd支持的对象-关系远程持久映射层。
它还提供了一个低级异步API,用于通用Twisted etcd应用程序,绕过库的对象-关系层。
安装
实现是纯Python代码,兼容Python 2和3,在PyPy上运行完美。在服务器端,需要使用版本3.1或更高版本的etcd。要安装txaioetcd
pip install txaioetcd
入门指南
从以下链接获取入门示例的完整源代码: 这里。
要使用高级、远程持久化映射API开始使用txaioetcd,至少定义一个用于持久化的数据类,例如一个User类。
class User(object):
def marshal(self):
"""
Marshal the object into a generic host language object.
"""
@staticmethod
def unmarshal(obj):
"""
Parse a generic host language object into a native object of this class.
"""
然后定义一个用于与键值存储一起使用的槽位表
from txaioetcd import pmap
# users table schema (a table with UUID keys and CBOR values holding User objects)
tab_users = pmap.MapUuidCbor(1, marshal=lambda user: user.marshal(), unmarshal=User.unmarshal)
上面的代码定义了一个槽位表(索引为1),其中键使用UUID,值为User类的CBOR序列化对象。
持久化映射的键和值的可用类型包括
字符串(UTF8),例如MapUuidString、MapStringString、MapStringUuid等。
二进制,例如MapUuidBinary、MapStringBinary等。
OID(uint64),例如MapUuidOid、MapOidCbor等。
UUID(uint128),例如MapUuidCbor、MapUuidUuid等。
JSON,例如MapUuidJson、MapOidJson、MapStringJson等。
CBOR,例如MapOidCbor、MapUuidCbor、MapStringCbor等。
Pickle(Python),例如MapStringPickle等。
Flatbuffers,例如MapUuidFlatbuffers等。
例如,以下是一个有效的槽位定义
# users table schema (a table with OID keys and Python Pickle values holding User objects)
tab_users = pmap.MapOidPickle(2, marshal=lambda user: user.marshal(), unmarshal=User.parse)
上面的代码定义了一个槽位表(索引为2),其中键使用OID,值为User类的Python Pickle序列化对象。
连接
首先打开与etcd的连接作为后端存储
from txaioetcd import Client, Database
etcd = Client(reactor, url='http://localhost:2379')
db = Database(etcd)
检查数据库连接
revision = await db.status()
print('connected to etcd: revision', revision)
存储和加载对象
现在创建一个来自上述类的本地Python对象并将其存储在表中,即在远程etcd中
user = User()
user.name = 'foobar'
user.oid = uuid.uuid4()
# create an async writable transaction to modify etcd data
async with db.begin(write=True) as txn:
tab_users[txn, user.oid] = user
# data is committed when transaction leaves scope .. here
print('user stored: {}'.format(user))
从表中加载一个本地Python对象,即在远程etcd中
# create an async read-only transaction when only accessing data in etcd
async with db.begin() as txn:
user = await tab_users[txn, user.oid]
print('user loaded: {}'.format(user))
组合起来
要将所有部分组合起来并运行代码,可以使用以下模板
import txaio
txaio.use_twisted()
from twisted.internet.task import react
from twisted.internet.defer import ensureDeferred
from txaioetcd import Client, Database
async def main(reactor):
etcd = Client(reactor, url='http://localhost:2379')
db = Database()
revision = await db.status()
print('connected to etcd: revision', revision)
# INSERT YOUR CODE HERE
def _main():
return react(
lambda reactor: ensureDeferred(
main(reactor)
)
)
if __name__ == '__main__':
txaio.start_logging(level='info')
_main()
在上面的占位符中插入操作etcd的代码。
项目详情
下载文件
下载适用于您平台的自定义文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建版本
txaioetcd-18.10.2.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1d16ba6e7ce55913baab5f763e1148ca83e7eb55aafacff8ef1ec6a2e9b83555 |
|
MD5 | ac7d78f6c4fc2c8ce7a45c17c092534b |
|
BLAKE2b-256 | 93a6319da1a5fddb29aad7e43b75ffe3ae5bb5c40136a5cfbab694e3f5831756 |
txaioetcd-18.10.2-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ce87048205183590fc8af7681bcd312b89290f5120066feb1ec9bb8027b631d0 |
|
MD5 | a107e24ab86dde29e0b21ff4ebf12a76 |
|
BLAKE2b-256 | b3bf842345fc2ff02e83028214511ff9deb38b6e731c12f7325777525eb795b5 |