@@ -65,15 +65,14 @@ need to *yield* the result of each API call. This client is available in
6565 self .foo = None
6666 loop.add_callback(self .watch)
6767
68- @coroutine
69- def watch (self ):
68+ async def watch (self ):
7069 c = Consul()
7170
7271 # asynchronously poll for updates
7372 index = None
7473 while True :
7574 try :
76- index, data = yield c.kv.get(' foo' , index = index)
75+ index, data = await c.kv.get(' foo' , index = index)
7776 if data is not None :
7877 self .foo = data[' Value' ]
7978 except Timeout:
@@ -96,32 +95,30 @@ result of each API call. This client is available in *consul.aio*.
9695
9796.. code :: python
9897
99- import asyncio
100- import consul.aio
98+ import asyncio
99+ import consul.aio
101100
101+ loop = asyncio.get_event_loop()
102102
103- loop = asyncio.get_event_loop()
103+ async def go ():
104104
105- @asyncio.coroutine
106- def go ():
105+ # always better to pass ``loop`` explicitly, but this
106+ # is not mandatory, you can relay on global event loop
107+ c = consul.aio.Consul(loop = loop)
107108
108- # always better to pass ``loop`` explicitly, but this
109- # is not mandatory, you can relay on global event loop
110- c = consul.aio.Consul( port = consul_port, loop = loop)
109+ # set value, same as default api but with ``yield from``
110+ response = await c.kv.put( ' foo ' , ' bar ' )
111+ assert response is True
111112
112- # set value, same as default api but with ``yield from``
113- response = yield from c.kv.put( b ' foo' , b ' bar ' )
114- assert response is True
113+ # get value
114+ index, data = await c.kv.get( ' foo' )
115+ assert data[ ' Value ' ] == b ' bar '
115116
116- # get value
117- index, data = yield from c.kv.get( b ' foo ' )
118- assert data[ ' Value ' ] == b ' bar '
117+ # delete value
118+ response = await c.kv.delete( ' foo2 ' )
119+ assert response is True
119120
120- # delete value
121- response = yield from c.kv.delete(b ' foo2' )
122- assert response is True
123-
124- loop.run_until_complete(go())
121+ loop.run_until_complete(go())
125122
126123
127124 Wanted
0 commit comments