纯 Python 的 etcd3 client - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
revol
V2EX    Python

纯 Python 的 etcd3 client

  •  
  •   revol
    revolution1 2018-03-22 12:58:58 +08:00 7361 次点击
    这是一个创建于 2850 天前的主题,其中的信息可能已经有所发展或是发生改变。

    发现没有好用的就自己写了一个

    • 基于 etcd3 的 gRPC-JSON-Gateway
    • 90% 的测试覆盖率
    • 同时兼容 python2 python3
    • 提供了 Lock Transaction Watch Lease 等几种 API 设计友好,使用优雅的工具

    Github 地址: https://github.com/Revolution1/etcd3-py

    文档地址: https://etcd3-py.readthedocs.io/en/latest/

    Quick Start

    Install

    $ pip install etcd3-py 

    Sync Client

    >>> from etcd3 import Client >>> client = Client('127.0.0.1', 2379) >>> client.version() EtcdVersion(etcdserver='3.3.0-rc.4', etcdcluster='3.3.0') >>> client.put('foo', 'bar') etcdserverpbPutResponse(header=etcdserverpbResponseHeader(cluster_id=11588568905070377092, member_id=128088275939295631, revision=15433, raft_term=4)) >>> client.range('foo').kvs [mvccpbKeyValue(key=b'foo', create_revision=15429, mod_revision=15433, version=5, value=b'bar')] 

    Async Client (Python3.5+)

    >>> import asyncio >>> from etcd3 import AioClient >>> client = AioClient('127.0.0.1', 2379) >>> async def getFoo(): ... await client.put('foo', 'bar') ... r = await client.range('foo') ... print('key:', r.kvs[0].key, 'value:', r.kvs[0].value) >>> loop = asyncio.get_event_loop() >>> loop.run_until_complete(getFoo()) key: b'foo' value: b'bar' 

    Transaction Util

    >>> from etcd3 import Client >>> txn = Client().Txn() >>> txn.compare(txn.key('foo').value == 'bar') >>> txn.success(txn.put('foo', 'bra')) >>> txn.commit() etcdserverpbTxnResponse(header=etcdserverpbResponseHeader(cluster_id=11588568905070377092, member_id=128088275939295631, revision=15656, raft_term=4), succeeded=True, respOnses=[etcdserverpbResponseOp(response_put=etcdserverpbPutResponse(header=etcdserverpbResponseHeader(revision=15656)))]) 

    Lease Util

    >>> from etcd3 import Client >>> client = Client() >>> with client.Lease(ttl=5) as lease: ... client.put('foo', 'bar', lease=lease.ID) ... client.put('fizz', 'buzz', lease=lease.ID) ... r = lease.time_to_live(keys=True) ... assert set(r.keys) == {b'foo', b'fizz'} ... assert lease.alive() 

    Watch Util

    >>> from etcd3 import Client >>> client = Client() >>> watcher = c.Watcher(all=True, progress_notify=True, prev_kv=True) >>> w.onEvent('f.*', lambda e: print(e.key, e.value)) >>> w.runDaemon() >>> # etcdctl put foo bar >>> # etcdctl put foz bar b'foo' b'bar' b'foz' b'bar' >>> w.stop() 

    Lock Util

    >>> import time >>> from threading import Thread >>> from etcd3 import Client >>> client = Client() >>> name = 'lock_name' >>> def user1(): ... with client.Lock(name, lock_ttl=5): ... print('user1 got the lock') ... time.sleep(5) ... print('user1 releasing the lock') >>> def user2(): ... with client.Lock(name, lock_ttl=5): ... print('user2 got the lock') ... time.sleep(5) ... print('user2 releasing the lock') >>> t1 = Thread(target=user1, daemon=True) >>> t2 = Thread(target=user2, daemon=True) >>> t1.start() >>> t2.start() >>> t1.join() >>> t2.join() user1 got the lock user1 releasing the lock user2 got the lock user2 releasing the lock 

    欢迎使用、顺手 star

    4 条回复    2018-08-07 11:34:27 +08:00
    BBCCBB
        1
    BBCCBB  
       2018-03-22 13:54:53 +08:00
    支持
    uhayate
        2
    uhayate  
       2018-03-22 14:09:34 +08:00 via iPhone
    支持
    jamiefang
        3
    jamiefang  
       2018-03-24 20:11:13 +08:00
    支持
    gomars
        4
    gomars  
       2018-08-07 11:34:27 +08:00
    可靠吗
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     900 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 21:24 PVG 05:24 LAX 13:24 JFK 16:24
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86