Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@vwxyzjn 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

This comment has been minimized.

Copy link

@shivekkhurana shivekkhurana commented Jun 22, 2017

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

This comment has been minimized.

Copy link

@shivekkhurana shivekkhurana commented Jun 25, 2017

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
You can’t perform that action at this time.