Top Python Libraries

Top Python Libraries

Unveiling Python Coroutines(Python Core in Action 18)

Explore the power of Python coroutines for asynchronous programming. Learn coroutine basics, task handling, and compare with multithreading through practical examples.

Meng Li's avatar
Meng Li
Sep 03, 2024
∙ Paid
1
Share

Welcome to the "Python Core in Action" Series

Table of Contents

Table of Contents

Meng Li
·
July 12, 2024
Read full story

Coroutines are a way to implement concurrent programming.

When you think of concurrency, you probably think of multithreading or multiprocessing. That's right—multithreading and multiprocessing are classic models for handling concurrency.

In the early days of the internet, multithreading and multiprocessing were crucial for server concurrency.

But as the internet grew rapidly, the C10K problem emerged—handling 10,000 simultaneous client connections.

This caused many systems to crash. Context switching between processes consumed a lot of resources, and threads couldn't handle the load. That’s when NGINX, with its event loop, came to the rescue.

The event loop starts a unified scheduler that decides which task to run at any given time. This approach avoids the overhead of starting threads, managing them, and using locks.

NGINX, at that time, maintained low resource usage and high performance under heavy concurrency, outperforming Apache in handling more simultaneous connections.

Later, a term called "callback hell" became popular. Anyone who has worked with JavaScript knows what this means.

We were thrilled to discover that coroutines inherit the advantages of the event loop while offering the `async`/`await` syntax, which improves both execution and readability.

As a result, coroutines gained more attention, and more people began using Node.js for backend development. (Here’s a joke: JavaScript is a programming language.)

Let’s start with a web scraping example. We’ll explain the concept step by step, using a real-world example to clarify this not-so-easy-to-understand topic. Afterward, we'll dive deeper into the core of coroutines.

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Meng Li
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture