PYTHON ADVANCED: PROFESSIONAL ENGINEERING MASTERY / L08ASYNC/AWAIT FUNDAMENTALS: NON-BLOCKING PYTHON
课程 · 12 · 08 / 12
LESSON 08 · ADVANCED · 60 MIN · ◆ 2 INSTRUMENTS

Async/Await Fundamentals: Non-Blocking Python

Understand event loops and coroutines. Write async code with asyncio, handle concurrent I/O, and build async patterns for production.

TIP

Learning Objectives: After this lesson, you'll understand event loops and coroutines, write async code with asyncio, handle concurrent I/O operations, and build async patterns for production applications.

Understanding Async Programming

Async programming allows handling many I/O operations concurrently without threads. Let's visualize the event loop:

FIG. 02Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 02Interactive flow diagrams, timelines, and process visualizations

The event loop runs coroutines one at a time. When one hits await, it pauses and another can run. This is cooperative multitasking.

The Problem with Blocking I/O

FIG. 04Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 04Interactive Python code execution environment

Async Solution Preview

FIG. 06Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 06Interactive Python code execution environment

Coroutines and async def

FIG. 08Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 08Interactive Python code execution environment

await Keyword

FIG. 10Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 10Interactive Python code execution environment

Concurrent Execution

FIG. 12Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 12Interactive flow diagrams, timelines, and process visualizations

asyncio.gather - Run Multiple Coroutines

FIG. 14Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 14Interactive Python code execution environment

asyncio.create_task - Background Tasks

FIG. 16Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 16Interactive Python code execution environment

asyncio.wait - Advanced Control

FIG. 18Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 18Interactive Python code execution environment

asyncio.as_completed - Process as They Finish

FIG. 20Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 20Interactive Python code execution environment

Error Handling

FIG. 22Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 22Interactive Python code execution environment

Timeouts and Cancellation

FIG. 24Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 24Interactive Python code execution environment

Async Context Managers and Iterators

FIG. 26Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 26Interactive Python code execution environment

Async Generators

FIG. 28Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 28Interactive Python code execution environment

Practical Patterns

Semaphore - Rate Limiting

FIG. 30Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 30Interactive Python code execution environment

Queue-based Worker Pool

FIG. 32Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 32Interactive Python code execution environment

Async HTTP Requests (aiohttp pattern)

FIG. 34Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 34Interactive Python code execution environment

Practice Exercises

Exercise 1: Async Retry with Backoff

FIG. 36Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 36Interactive Python code execution environment

Exercise 2: Async Resource Pool

FIG. 38Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 38Interactive Python code execution environment

Key Takeaways

ConceptDescription
CoroutineFunction defined with async def
awaitPause and wait for async operation
Event LoopManages all async operations
TaskScheduled coroutine for concurrent execution
gatherRun multiple coroutines concurrently
SemaphoreLimit concurrent operations
QueueAsync producer-consumer pattern

When to Use Async

ScenarioUse Async?
Many network requestsYes
Database queriesYes (with async driver)
File I/OMaybe (use aiofiles)
CPU-heavy workNo (use multiprocessing)
Real-time eventsYes
Simple scriptsUsually overkill

Next Steps

In the next lesson, we'll explore Testing Best Practices—master pytest with fixtures, parametrization, and mocking, learn TDD, and write maintainable tests.


Ready to write bulletproof code? Testing awaits!


Further Reading

Required Reading

Foundational Talks & Articles

Modern Async Stack

  • asyncio.TaskGroup — Python 3.11's structured concurrency primitive. Use this instead of bare gather.
  • trio — alternative async framework with first-class structured concurrency.
  • anyio — write code that runs on both asyncio and trio. The async library library.
  • httpx — sync + async HTTP. The modern requests.
  • aiohttp — battle-tested async HTTP server + client.
  • asyncpg — fastest async PostgreSQL driver in any language.

Books

  • Book: Fluent Python (2nd ed.) — Chapters 19 ("Concurrency Models") and 21 ("Asynchronous Programming"). Best treatment in print.
  • Book: Python Concurrency with asyncio — Matthew Fowler. Whole book on the topic.