Get started with async in Python

Posted on 21-11-2019 , by: admin , in , 0 Comments

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.