Skip to content

Instantly share code, notes, and snippets.

@ianchen06
Forked from shivekkhurana/main.py
Created May 25, 2018 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianchen06/b5c9c9bf506e738611804447cc46cba0 to your computer and use it in GitHub Desktop.
Save ianchen06/b5c9c9bf506e738611804447cc46cba0 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