Get started with async in Python

Learn how to use asynchronous programming in Python to get more done in less time, without waiting

Get started with async in Python
graemenicholson / Getty Images

Asynchronous programming, or async for short, is a feature of many modern languages that allows a program to juggle multiple operations without waiting or getting hung up on any one of them. It’s a smart way to efficiently handle tasks like network or file I/O, where most of the program’s time is spent waiting for a task to finish.

Consider a web scraping application that opens 100 network connections. You could open one connection, wait for the results, then open the next and wait for the results, and so on. Most of the time the program runs is spent waiting on a network response, not doing actual work.

Async gives you a more efficient method: Open all 100 connections at once, then switch among each active connection as they return results. If one connection isn’t returning results, switch to the next one, and so on, until all connections have returned their data.

Async syntax is now a standard feature in Python, but longtime Pythonistas who are used to doing one thing at a time may have trouble wrapping their heads around it. In this article we’ll explore how asynchronous programming works in Python, and how to put it to use.

Note that if you want to use async in Python, it’s best to use Python 3.7 or Python 3.8 (the latest version as of this writing). We’ll be using Python’s async syntax and helper functions as defined in those versions of the language.

To continue reading this article register now