Async HTTP Requests with Aiohttp & Aiofiles

Handle hundreds of HTTP requests, disk writes, and other I/O-bound tasks with quintessential async Python libraries.

Todd Birchard
13 min readJan 18, 2022

When building applications within the confines of a single-threaded, synchronous language, the limitations become very obvious very quickly. The first thing that comes to mind is writes: the very definition of an I/O-bound task. When writing data to files (or databases), each “write” action intentionally occupies a thread until the write is complete. This makes a lot of sense for ensuring data integrity in most systems. For example, if two operations simultaneously attempt to update a database record, which one is correct? Alternatively, if a script requires an HTTP request to succeed before continuing, how could we move on until we know the request succeeded?

HTTP requests are among the most common thread-blocking operations. When we write scripts that expect data from an external third party, we introduce a myriad of uncertainties that can only be answered by the request itself, such as response time latency, the nature of data we expect to receive, or if the request will succeed. Even when working with APIs we’re confident in, no operation is sure to succeed until it’s complete. Hence, we’re “blocked.”

--

--

Todd Birchard

Software engineer with background in Data & Product Management background. Does everything incorrectly before coming to realizations known as best practices.