Skip to content

Instantly share code, notes, and snippets.

@dalanmiller
Created May 2, 2016 22:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalanmiller/a4c7c3b180f51fba3ed40edb4f239c79 to your computer and use it in GitHub Desktop.
Save dalanmiller/a4c7c3b180f51fba3ed40edb4f239c79 to your computer and use it in GitHub Desktop.
asyncio & RethinkDB changefeeds example
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()
@vwxyzjn
Copy link

vwxyzjn commented Jun 21, 2017

I am running the code in python 3.6 and it says
'async for' requires an object with aiter method, got AsyncioCursor

@shivekkhurana
Copy link

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 ?

@shivekkhurana
Copy link

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