-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasyncio_rethinkdb_changefeed.py
41 lines (31 loc) · 1 KB
/
asyncio_rethinkdb_changefeed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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()