Skip to content

Instantly share code, notes, and snippets.

@kristoff-it
Last active September 18, 2019 13:44
Show Gist options
  • Save kristoff-it/84b67ca5fa2d62f0d307f6cca62808c2 to your computer and use it in GitHub Desktop.
Save kristoff-it/84b67ca5fa2d62f0d307f6cca62808c2 to your computer and use it in GitHub Desktop.
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())
@Riezebos
Copy link

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

@kristoff-it
Copy link
Author

kristoff-it commented Sep 11, 2019

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