Kick off your book project in 3 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Saturday, May 16, 2026. Learn more…

Leanpub Header

Skip to main content

asyncio from ground up

A working Mental Model for Python asyncio

This book is 100% completeLast updated on 2026-05-04

Most asyncio tutorials introduce async and await on page one and ask you to take the runtime on faith. This book does it the other way around. You build a working event loop in

thirty lines of plain Python — no asyncio import — and meet the keywords as labels for parts of a runtime you have already watched run. By the end, async Python stops being

intimidating and starts being readable.

Interested in this book? Show your support by saying what you'd like to pay for it!

$
PDF
EPUB
WEB
About

About

About the Book

You have probably tried asyncio once and given up. You may have searched "python asyncio tutorial" and bounced off three of them, or watched senior engineers go quiet the moment `await` appears in a code review. None of those experiences is unusual, and none of them is your fault.

The reason most readers find asyncio confusing is that the topic is taught keywords-first. The first page of a typical tutorial introduces `async def`, the second introduces `await`, and the reader is expected to imagine the runtime underneath. A keyword without a runtime is a name without a thing. The reader, fairly, gives up. After a few rounds of giving up, async code becomes "the topic I avoid", and the avoidance hardens into a wall.

This book is that wall coming down.

THE APPROACH

asyncio is small. A loop that runs the next ready job. A way to pause a function in the middle and resume it later. A handful of keywords that name parts of that mechanism. The difficulty is in the order topics are usually taught. We do them in the opposite order.

Part I (Chapters 1 to 2) names the felt problem. A blocking script that freezes for twelve seconds while it fetches six URLs one after another. The threaded version that helps but does not scale.

Part II (Chapters 3 to 6) builds the runtime in plain Python. Chapter 3 hand-rolls a working event loop in 30 lines, no asyncio import in sight. Chapters 4 and 5 introduce `async`, `await`, and `asyncio.run` as labels for parts of a runtime you have already watched run. Chapter 6 takes the lid off at the bytecode level for readers who want depth.

Part III (Chapters 7 to 10) is the production toolbox: `gather`, `create_task`, `TaskGroup`, the time toolbox, async iteration, structured concurrency, cancellation, exception groups, sync primitives, and `contextvars`.

Part IV (Chapters 11 to 12) takes you to production. Chapter 11 is a guided debugging session for the most common asyncio failure modes. Chapter 12 covers testing async code with `pytest-asyncio` and `AsyncMock`, including the silent-pass bug that makes broken tests look green.

Part V (Chapter 13) is where asyncio ends. Executors, sync↔async bridges, graceful shutdown on SIGINT and SIGTERM.

THE RUNNING EXAMPLES

A single CLI script, `news.py`, evolves chapter by chapter from a blocking version that takes twelve seconds into a production-shaped program with structured concurrency, semaphore-capped fan-out, trace IDs, and graceful shutdown. A parallel example, `tcpcheck.py`, applies the same asyncio primitives to raw TCP. You leave with two working scripts you could ship.

WHO THIS BOOK IS FOR

Python developers who have been avoiding asyncio. Backend engineers writing services that talk to APIs, databases, and queues. CLI authors making scripts that fetch from multiple sources. Anyone who has read about the GIL and come away thinking Python is broken for concurrent programming.

If you are building GenAI applications, this book is essential. Every LLM-powered system is I/O orchestration at heart: parallel calls to a model provider, streaming tokens back to clients, fan-out to embeddings and vector searches, rate-limited retries, agent loops that keep WebSockets open while tool calls resolve. asyncio is what makes that orchestration scalable and performant. A GenAI service written without asyncio leaves most of its throughput on the table.

You should already know functions, classes, imports, and how to read a stack trace. You do not need to know what an event loop is, what `__await__` does, or how `epoll` works underneath the loop. The book teaches all three.

WHAT YOU WILL LEAVE WITH

You will read async Python without flinching. The line that is a keyword and the line that is doing real work will be obvious to you at a glance. You will write your own async programs, recognize when asyncio is the wrong tool, and reach for the right one instead. asyncio's place in a design conversation will be a question you can answer rather than a question you change the subject to.

The technology is small. The barrier was the order in which it has been taught to you. Time to do it the other way around.

Author

About the Author

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.

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.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

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