python-consul2
python-consul2 copied to clipboard
Asyncio example does not run
I was investigating using this library. I've had Consul running and tried to run the asyncio example script from the docs by copying and pasting it into a file.
I expected it to run but if failed and reported the following error:
demo.py:8: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
def go():
Traceback (most recent call last):
File "demo.py", line 26, in <module>
loop.run_until_complete(go())
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "demo.py", line 15, in go
response = yield from c.kv.put(b'foo', b'bar')
File "/consul-demo/venv/lib/python3.8/site-packages/consul/base.py", line 3018, in put
assert not key.startswith('/'), \
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
The error is caused because the put function has an assert statement to check whether the key begins with the string '/'. However, the arguments being passed to the function are bytes which causes the error.
Arguments used in other example scripts are all strings. I'm assuming that the API used to take either bytes or strings but changed to only strings at some point and this particular example script did not get updated to reflect a recent change to only use strings.
It seems like a docs update is needed.
After fixing the input args problem mentioned above, by changing the args to strings (b'foo' -> 'foo' and b'bar' -> 'bar'), I encountered another two problems running the example script.
The first problem was that after calling the get function the returned value was actually a bytes object, whereas it was a string when the put function was called. I'm not sure what to recommend here - the behaviour is confusing. I have since run the synchronous example script from the docs (under the heading "Standard") and it too produces a bytes object - whereas the docs indicate that it returns a string.
The second problem was in the delete step which deletes key foo2. This fails - because the steps previously added a key called foo.
Change
response = yield from c.kv.delete(b'foo2')
to
response = yield from c.kv.delete('foo')
Looks like some bit-rot in the docs examples.
- Because python2 response.content return a str ,when python3 response.content return bytes You are right ,the document has not been updated or differentiated accordingly, so please do not worry that it will be revised in the future @claws
- Hey guy, would you be interested in giving me a PR
Which part is in need of a pull request? I was able to compile the asyncio example without editing the library. Is it just the docs?
Pretty sure it is just a docs problem. When a user tries to follow them they result in an error.
Yea, I copied the code from the old repository before finding this. I couldn't really get that compile without editing the example. I'll look into this if I have some free time.