跳转到主要内容

etcd3的异步客户端库

项目描述

Version Build Status Docs

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),例如MapUuidStringMapStringStringMapStringUuid等。

  • 二进制,例如MapUuidBinaryMapStringBinary等。

  • OID(uint64),例如MapUuidOidMapOidCbor等。

  • UUID(uint128),例如MapUuidCborMapUuidUuid等。

  • JSON,例如MapUuidJsonMapOidJsonMapStringJson等。

  • CBOR,例如MapOidCborMapUuidCborMapStringCbor等。

  • 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 (39.3 kB 查看哈希值)

上传时间 源代码

构建版本

txaioetcd-18.10.2-py2.py3-none-any.whl (48.0 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下机构支持