Skip to content

Instantly share code, notes, and snippets.

@shivekkhurana
Created June 25, 2017 07:36
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save shivekkhurana/1de00e1e54c36d250a7f19905fe133b9 to your computer and use it in GitHub Desktop.
Save shivekkhurana/1de00e1e54c36d250a7f19905fe133b9 to your computer and use it in GitHub Desktop.
Listen to multiple rethinkdb change feeds with python(3.6) using asyncio
import rethinkdb as r
import asyncio
from typing import Callable, Dict
r.set_loop_type('asyncio')
async def get_connection():
return await r.connect(
db='test',
host='localhost'
)
async def set_change_handler(table_name: str, handler: Callable) -> None:
print('Listening for changes on {}'.format(table_name))
connection = await get_connection()
feed = await r.table(table_name).changes().run(connection)
while (await feed.fetch_next()):
change = await feed.next()
handler(change)
print('Got a change on table: {}; {}'.format(table_name, change))
def get_handler_map():
# add any table_name: handler_func here
return {
'events': print,
'actions_status': print,
'triggers_status': print
}
def main():
loop = asyncio.get_event_loop()
for table_name, handler in get_handler_map().items():
loop.create_task(set_change_handler(table_name, handler))
loop.run_forever()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment