Last active
September 18, 2019 13:44
-
-
Save kristoff-it/84b67ca5fa2d62f0d307f6cca62808c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aioredis | |
async def add_new_win(pool, winner): | |
await pool.zincrby('wins_counter', 1, winner) | |
await pool.incr('total_games_played') | |
async def main(): | |
# Connect to Redis | |
pool = await aioredis.create_redis_pool('redis://localhost', encoding='utf8') | |
# Tail the event stream | |
last_id = '$' | |
while True: | |
events = await pool.xread(['wins_stream'], latest_ids=[last_id], timeout=0, count=10) | |
# Process each event by calling `add_new_win` | |
for _, e_id, e in events: | |
winner = e['winner'] | |
await add_new_win(pool, winner) | |
last_id = e_id | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
Thank you very much for pointing it out!
I heard that compilers and interpreters are grammar nerds too :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Kristoff,
I like your async await article!
Maybe I'm a grammar nerd, but I suspect line 24 should be changed from:
loop.run_unitl_complete(main())
to:
loop.run_until_complete(main())
😃
Cheers,
Simon