@@ -65,15 +65,14 @@ need to *yield* the result of each API call. This client is available in
65
65
self .foo = None
66
66
loop.add_callback(self .watch)
67
67
68
- @coroutine
69
- def watch (self ):
68
+ async def watch (self ):
70
69
c = Consul()
71
70
72
71
# asynchronously poll for updates
73
72
index = None
74
73
while True :
75
74
try :
76
- index, data = yield c.kv.get(' foo' , index = index)
75
+ index, data = await c.kv.get(' foo' , index = index)
77
76
if data is not None :
78
77
self .foo = data[' Value' ]
79
78
except Timeout:
@@ -96,32 +95,30 @@ result of each API call. This client is available in *consul.aio*.
96
95
97
96
.. code :: python
98
97
99
- import asyncio
100
- import consul.aio
98
+ import asyncio
99
+ import consul.aio
101
100
101
+ loop = asyncio.get_event_loop()
102
102
103
- loop = asyncio.get_event_loop()
103
+ async def go ():
104
104
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)
107
108
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
111
112
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 '
115
116
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
119
120
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())
125
122
126
123
127
124
Wanted
0 commit comments