Company
Date Published
Author
Redis
Word count
1334
Language
English
Hacker News points
None

Summary

This post discusses the basics of async/await in Python, focusing on improving program throughput by reducing idle time when performing I/O operations. The author chooses to use Python 3 as an example since its concurrency primitives are relatively recent and may not be familiar to many users. The main reason for using async/await is to improve performance in programs that do a lot of network communication, such as those connected to a Redis database. The author provides a code sample that tracks wins for a hypothetical game, updating a Redis Sorted Set that acts as the leaderboard, and then shows an equivalent non-blocking version of the code using aio-libs/aioredis. However, the initial non-blocking version does not improve performance due to overusing the await keyword, which blocks execution flow. The author addresses this by introducing asyncio.gather, a synchronization primitive that allows scheduling multiple tasks concurrently. By applying asyncio.gather, the program can now process each batch of events concurrently, improving throughput without significant changes to the code.