Leanpub Header

Skip to main content
Go to Leanpub.comLeanpub Store Events Authors Readers Organizations Services

Asyncio Without the Magic

From Zero to Async Python You Actually Understand

Learn Python asyncio from zero by building the event loop by hand. Six fetches go from 12 seconds to 3 on one thread, and you understand every line. Most asyncio tutorials throw async and await at you on page one. This course builds the event loop by hand first, in plain Python, so the keywords finally click. Six fetches drop from 12 seconds to 3, on one thread, and you understand every line. No magic.

This interactive course has a companion book, Asyncio Without the Magic, on Leanpub at https://leanpub.com/asyncio . The book is the deep, cover-to-cover read; the course is where you make it stick with quizzes, build-it-yourself exercises, and runnable notebooks. They are built to be used together: read the idea in the book, then drill it in the course until it is yours. You get a lot from either one on its own, and the most by far from both together.

Learn Python asyncio from zero by building the event loop by hand. Six fetches go from 12 seconds to 3 on one thread, and you understand every line. Most asyncio tutorials throw async and await at you on page one. This course builds the event loop by hand first, in plain Python, so the keywords finally click. Six fetches drop from 12 seconds to 3, on one thread, and you understand every line. No magic.

This interactive course has a companion book, Asyncio Without the Magic, on Leanpub at https://leanpub.com/asyncio . The book is the deep, cover-to-cover read; the course is where you make it stick with quizzes, build-it-yourself exercises, and runnable notebooks. They are built to be used together: read the idea in the book, then drill it in the course until it is yours. You get a lot from either one on its own, and the most by far from both together.

Minimum price

45ドル.00

59ドル.00

You pay

Author earns

$

Also available for 1 course credit with a Learner Membership

PDF
EPUB
WEB
About
  1. About
  2. Instructor
  3. Material
Asyncio Without the Magic

About

About the Course

Run a small Python program that fetches six things from the internet, and you wait. The screen sits there, frozen, for about twelve seconds while the results crawl in one at a time. Nothing is broken, and your computer is not slow. The program is just waiting, and while it waits, it refuses to do anything else.

There is a way to make those same six finish in about three seconds, on a single thread, with no faster computer and no extra processors. It is called asyncio, and it has a reputation for being hard. That reputation comes almost entirely from how it is taught: async on page one, await on page two, and a pile of machinery you are asked to trust on faith.

This course does the opposite. You build the machinery first, by hand, in plain Python you can read. You write a tiny event loop with generators, watch it run six fake fetches in three seconds on one thread, and only then meet async, await, gather, tasks, and the rest as names for parts you already built. The model is not in the keywords. The model is in a small loop you write yourself, and once you have written it the rest of asyncio reads like a polished version of your own code.

From there you make it real and make it safe: timeouts on every wait, cancellation that cleans up after itself, a semaphore that caps load so one thousand connections do not melt your program, async context managers that never leak a session. You learn to debug a frozen async program in minutes by recognizing the one cause and the three warnings everyone hits, and to test async code without shipping the silent kind of bug that passes green while checking nothing.

By the end you can read async Python anywhere and see the event loop underneath it. You will know exactly where asyncio helps (waiting on the network, a database, thousands of slow connections) and exactly where it does not (heavy computation), so you never reach for it wrongly. That is the working command of asyncio that most people never reach, because they were handed the keywords without the model. You will have both.

This course is the hands-on half of a pair. Its companion book, Asyncio Without the Magic, on Leanpub at https://leanpub.com/asyncio , walks the same path as one flowing read you can take cover to cover. The course turns that read into practice: graded quizzes, exercises you solve yourself with worked answers, and notebooks you run in your browser. Use the book to absorb the model and the course to prove you own it, and you get the deepest result of all.

Instructor

About the Instructor

Ritesh Modi

Ritesh Modi is Head of AI at MarketOnce and a former Forward Deployed Engineer at Microsoft. He has spent more than a decade building and shipping production systems across cloud, distributed computing, and applied machine learning, working with organizations ranging from global enterprises to fast-moving startups. His recent work focuses on applied large language models, designing systems that turn pretrained models into reliable, task-specific tools.

Ritesh has authored multiple technology books and speaks regularly at industry conferences on AI, cloud architecture, and software engineering. His writing philosophy rests on a simple belief: the best technical books are written by practitioners who still remember what it felt like to not understand something, not by experts who have forgotten. Every explanation in this book was tested against that standard, if it would not have made sense to him when he was first learning this material, it was rewritten until it did.

He writes, shares ideas, and connects with readers at www.riteshmodi.com. When he is not writing or building AI systems, he can be found mentoring engineers, exploring new architectures, or debugging a training run that should have converged three hours ago.

Material

Course Material

  • Welcome: Twelve Seconds, Then Three

  • Let’s name the fear first

  • What you need to know already (and what you don’t)

  • What you’ll be able to do at the end

  • Every lesson builds one part of the machine

  • How this course works

  • Exercise: How Your Score Is Counted

  • You’ll write a little code — here’s the deal

  • A promise

  • Read on...

  • Setup: Two Worlds, await and asyncio.run

  • First, three words you’ll use constantly

  • Pick ONE way to run the code

  • Track A — Google Colab (recommended, zero install)

  • Track B — VS Code (a real editor on your computer)

  • Track C — Local Jupyter (the classic notebook)

  • The one thing that’s different in a notebook

  • Smoke test: prove it works

  • Your turn

  • Exercise: Run Your First await

  • Exercise: Notebook or Script?

  • Check your understanding

  • Quiz: Setup Check

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • The Blocking Function

  • The intuition: a chef who stares at one pot

  • The idea, made precise: blocking

  • The smallest possible example

  • Now in code

  • Why this gets worse, not better

  • The question that drives everything

  • Your turn

  • Exercise: Predict the Blocking Total

  • Exercise: Wait or Compute?

  • Exercise: Build a 9-Second SITES List

  • Exercise: Would a Faster CPU Help?

  • Check your understanding

  • Quiz: Blocking

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • Concurrency, Parallelism, and the GIL

  • The intuition: one chef, many dishes

  • The idea, made precise

  • Threads, and the thing that surprises everyone

  • The GIL: a single baton

  • Where threads stop helping: computing

  • So why not just use threads?

  • Your turn

  • Exercise: Predict the Threaded Time

  • Exercise: Why Didn’t Two Threads Halve It?

  • Exercise: Four One-Second Waits on Four Threads

  • Exercise: The Four-Thread Sum That Won’t Speed Up

  • Check your understanding

  • Quiz: Concurrency, Parallelism & the GIL

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • The Loop, From Scratch

  • The intuition: a waiter at ten tables

  • A queue of jobs, and what "ready" means

  • Generators: a function that can pause

  • The loop

  • Run it: six fetches, one thread, three seconds

  • The four states every job lives in

  • Your turn

  • Exercise: Predict the Finish Order

  • Exercise: Fix the Loop That Didn’t Overlap

  • Exercise: Predict the Order, Then Verify

  • Exercise: Fix the Loop That Drops Waiting Jobs

  • Exercise: Build a Generator That Pauses Once

  • Check your understanding

  • Quiz: The Loop, From Scratch

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • Coroutines, Without the Magic

  • The intuition: a bookmark in a book

  • The idea, made precise

  • The surprise: calling a coroutine runs none of it

  • Making it actually run

  • It’s the same machine as Lesson 4

  • The trap, named now so it can’t bite later

  • Your turn

  • Exercise: Did the Body Run?

  • Exercise: Fix the "Never Awaited" Bug

  • Exercise: Predict the Coroutine Object

  • Exercise: Fix the Body That Never Ran

  • Check your understanding

  • Quiz: Coroutines

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • async and await

  • Where we are

  • async: this function is allowed to pause

  • await: run this, and pause me until it’s done

  • What you’re allowed to await: awaitables

  • What you cannot await, and the exact error

  • The first async news.py — and a cliffhanger

  • Your turn

  • Exercise: Spot the Non-Awaitable

  • Exercise: Why Is the Async Version Still 12 Seconds?

  • Exercise: Predict, See, Then Fix the Non-Awaitable

  • Exercise: What Does await fetch() Promise the Loop?

  • Check your understanding

  • Quiz: async and await

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • Behind the Bookmark

  • Where the bookmark actually lives

  • What gets saved when a coroutine pauses

  • How the loop wakes it back up

  • A Task is a coroutine the loop is driving

  • For the curious: the send mechanics

  • For the curious: how the loop knows I/O is ready

  • Your turn

  • Exercise: Do the Locals Survive the Pause?

  • Exercise: Recipe or Cooking?

  • Exercise: What Runs, and When?

  • Exercise: Drive It By Hand

  • Check your understanding

  • Quiz: Behind the Bookmark

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • Getting Things Started: run, create_task, gather

  • Where does the loop even come from?

  • Sequential vs concurrent: the one idea that makes it fast

  • gather: start many, get results in order

  • The notebook gotcha, exercised

  • Your turn

  • Exercise: Make the Six Fetches Fast

  • Exercise: Trigger (and Fix) the Notebook Error

  • Exercise: Predict Concurrent vs Sequential, Then Run Both

  • Exercise: Does gather Return Finish Order or Call Order?

  • Exercise: Read the Notebook Error and Fix It

  • Check your understanding

  • Quiz: run, create_task, gather

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • The Time Toolbox

  • First, the most important sleep distinction in asyncio

  • wait_for: put a time limit on any awaitable

  • asyncio.timeout(): the modern context-manager form

  • Putting it together: one slow site can’t sink the batch

  • as_completed: handle each result as it lands

  • Your turn

  • Exercise: Predict the Timed-Out Batch

  • Exercise: Fix the Sleep That Killed Concurrency

  • Exercise: Predict the as_completed Order

  • Exercise: Predict What wait_for Raises

  • Check your understanding

  • Quiz: The Time Toolbox

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • async for and async with

  • The intuition: a conveyor belt

  • Async generators: async def with yield

  • async with: open and close, safely, with awaits

  • The leak this prevents

  • Your turn

  • Exercise: Predict the Arrival Order

  • Exercise: Does Cleanup Still Run on Error?

  • Exercise: Reorder the Sites, Predict Again

  • Exercise: Fix the Plain for

  • Check your understanding

  • Quiz: async for and async with

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • Tasks, TaskGroups, Cancellation, and Exception Groups

  • Where we are: coroutine vs Task, once more

  • TaskGroup: structured concurrency by construction

  • Cancellation: stopping a task on purpose

  • The bare-except trap

  • Exception groups: when several children fail at once

  • Capping concurrency: Semaphore

  • The producer/consumer pattern with Queue

  • Your turn

  • Exercise: Predict the Semaphore Total

  • Exercise: Work Backward to the Semaphore Size

  • Exercise: Fix the Swallowed Cancellation

  • Exercise: Predict, Then Run the Wave Count

  • Exercise: Cancel a Task and Report What Came Back

  • Check your understanding

  • Quiz: Tasks, TaskGroups, Cancellation, and Exception Groups

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • What Goes Wrong, and How to Find It

  • The intuition: one frozen chef stops the whole kitchen

  • Gotcha 1: a blocking call freezes the loop (the big one)

  • Gotcha 2-5, on one card

  • Decoding the three warnings

  • Seeing what every task is doing

  • Your turn

  • Exercise: Find the Frozen Loop

  • Exercise: Match the Warning to the Cause

  • Exercise: Fix the Hidden time.sleep

  • Exercise: Match Each Warning to Its Cause

  • Check your understanding

  • Quiz: Debugging asyncio

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • Testing Async Code

  • The intuition: a test that was never actually run

  • Running coroutine tests: pytest-asyncio

  • The AsyncMock vs MagicMock trap

  • The subtlest bug: the test that returns a coroutine

  • Your turn

  • Exercise: Run the Async Tests

  • Exercise: Pick the Right Mock

  • Exercise: Fix the Test That Tests Nothing

  • Exercise: Which Mock Is Awaitable?

  • Exercise: Explain the Silent Pass

  • Check your understanding

  • Quiz: Testing Async Code

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • Where asyncio Ends: Executors and Shutdown

  • The line: await is for waiting, not computing

  • to_thread: get blocking and CPU work off the loop

  • Bridging the two worlds

  • The notebook trick, finally explained: nest_asyncio

  • Graceful shutdown

  • Your turn

  • Exercise: Get the Blocking Work Off the Loop

  • Exercise: Will asyncio Help Here?

  • Exercise: Why the Thread Sets the Loop Free

  • Exercise: Workaround, Not a Fix

  • Check your understanding

  • Quiz: Where asyncio Ends

    3 attempts allowed

  • Recap in plain words

  • Read on...

  • Final Quiz and Where Async Takes You Next

  • What you can do now

  • An honest look at the limits

  • Where async takes you next

  • The final quiz

  • Final Quiz: Asyncio Without the Magic

    3 attempts allowed

  • Your certificate

  • One last word

  • Read on...

  • Asyncio in the Wild: Where You’ll Meet It Next

  • The four patterns, one more time

  • Talking to websites: httpx and aiohttp

  • Building a web server: FastAPI

  • Talking to a database: asyncpg and friends

  • Chat bots: discord.py, Telegram, and friends

  • Controlling a browser: Playwright

  • Calling an AI model: the OpenAI and Anthropic SDKs

  • What they all have in common

  • Your turn

  • Exercise: Spot the Patterns You Know

  • Exercise: Why Is This Library Async?

  • Exercise: Map Each Pattern to Its Real Job

  • Exercise: Predict the Real news.py

  • Check your understanding

  • Quiz: Asyncio in the Wild

    3 attempts allowed

  • Recap in plain words

  • Where to go from here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn 8ドル on a 10ドル Purchase, and 16ドル on a 20ドル Purchase

We pay 80% royalties on purchases of 7ドル.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between 0ドル.99 and 7ドル.98. You earn 8ドル on a 10ドル sale, and 16ドル on a 20ドル sale. So, if we sell 5000 non-refunded copies of your book for 20ドル, you'll earn 80,000ドル.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over 15ドル million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub

AltStyle によって変換されたページ (->オリジナル) /