🔃Unit 3: Concurrency
async
and await
are key concepts in Python that facilitate writing asynchronous code. They were introduced in Python 3.5 and have become central in writing efficient and non-blocking code, especially in the context of I/O-bound and high-level structured network code.
Synchronous and Asynchronous
Synchronous programming is simpler and more straightforward, best suited for linear tasks and CPU-bound operations. Asynchronous programming, on the other hand, allows for concurrent operations, making it ideal for tasks that involve waiting for external operations, thus improving efficiency and scalability in specific scenarios like web servers and UI applications.
Key differences:
Blocking vs. Non-Blocking: Synchronous programming is blocking, while asynchronous programming is non-blocking.
Control Flow: Synchronous code has a straightforward, top-to-bottom control flow. Asynchronous code, however, can be more complex due to callbacks and event loops.
Scalability: Asynchronous programming is more scalable, particularly for I/O-bound and network-bound operations.
Resource Utilization: Asynchronous programming can make better use of system resources, while synchronous programming may underutilize resources during wait times.
async
/await
Keywords
async
/await
Keywordsasync
Keyword
async
KeywordIn Python, the async
keyword is used to declare a function as an "asynchronous function." Such functions, known as "async functions," are able to "pause" their execution without blocking the entire thread by yielding control back to the event loop.
An async function defines a "coroutine" which is a type of function that can suspend its execution before reaching return
, and it can indirectly pass control back to the event loop. This suspension of execution is done using the await
keyword.
await
Keyword
await
KeywordWithin an async function, you use the await
keyword before a function that returns an "awaitable" object (like another async function). This tells Python to pause the async function until the awaitable is resolved. This pause is non-blocking; it allows other async functions to run in the meantime.
When an async function hits an await
keyword, it pauses and yields control back to the event loop, which can then go on to execute other tasks. Once the awaited operation is completed, the event loop resumes the execution of the async function from the point it was paused. In other words, Python will know that it can go and do something else in the meanwhile (like receiving another request).
For await
to work, it has to be inside a function that supports this asynchronicity. To do that, you just declare it with async def
.
Last updated