Created
May 2, 2016 22:39
-
-
Save dalanmiller/a4c7c3b180f51fba3ed40edb4f239c79 to your computer and use it in GitHub Desktop.
asyncio & RethinkDB changefeeds example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rethinkdb as r | |
import asyncio | |
r.set_loop_type("asyncio") | |
async def get_connection(): | |
return await r.connect("localhost", 28015) | |
async def changefeed_old(): | |
conn = await get_connection() | |
changes = await r.db("test").table("test").changes()["new_val"].run(conn) | |
async for change in changes: | |
print(change) | |
async def changefeed_new(): | |
conn = await get_connection() | |
changes = await r.db("test").table("test").changes()["old_val"].run(conn) | |
async for change in changes: | |
print(change) | |
loop = asyncio.get_event_loop() | |
loop.create_task(changefeed_old()) | |
loop.create_task(changefeed_new()) | |
loop.run_forever() |
Same error with 3.6.
TypeError: 'async for' requires an object with __aiter__ method, got AsyncioCursor
Is there a way to fix it ? What am I doing wrong ?
After reviewing a lot of amazing work by David Beazley (https://github.com/dabeaz), and with the improvements in async python (mainly introduction of async
/ await
keyword and inclusion of asyncio into the stdlib), we can do this very easily.
I have created a gist here with a working example
https://gist.github.com/shivekkhurana/1de00e1e54c36d250a7f19905fe133b9
It gives a simple structure to listen to multiple change feeds asynchronously (tested on python 3.6)
Hope it helps.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am running the code in python 3.6 and it says
'async for' requires an object with aiter method, got AsyncioCursor