Asynchronous programming emerged in Python starting from version 3.5 with the introduction of the keywords async and await. Initially, libraries such as asyncio and generator-based coroutines were used for asynchronous tasks, which were difficult to understand and maintain. With the introduction of the async/await syntax, asynchronous code became more explicit and readable, closer to the familiar synchronous style.
Before the advent of async/await, asynchrony was implemented through callbacks and generators (for instance, using the tornado library or older asyncio APIs). This code was challenging for debugging and maintenance.
The main issue with processing a large number of simultaneous I/O operations (network requests, file input/output) in synchronous code is the blocking of the main thread. This leads to decreased performance and an inability to efficiently utilize resources.
Asynchronous programming using async/await allows performing many I/O operations concurrently within a single thread, avoiding blockages. Meanwhile, the syntax is similar to regular functions, which improves readability and debugging.
import asyncio async def fetch_data(delay): print(f"Start fetching after {delay}s delay") await asyncio.sleep(delay) print(f"Done fetching after {delay}s delay") return delay async def main(): results = await asyncio.gather( fetch_data(1), fetch_data(2), fetch_data(3) ) print("Results:", results) asyncio.run(main())
Can a function defined with async def be called like a regular function?
No. Calling such a function returns a coroutine object, which does not execute until passed to an event loop (for instance, using await or asyncio.run()).
def foo(): return 42 async def bar(): return 42 print(foo()) # 42 print(bar()) # <coroutine object bar at ...>
Can await be used outside of an asynchronous function?
No. The await keyword should only be used inside functions declared with async def. Trying to use await outside of such a function will raise a SyntaxError.
# Error! await asyncio.sleep(1) # SyntaxError: 'await' outside async function
Does asynchrony work for operations not related to I/O (for example, computations)?
No. Asynchrony is effective only for I/O operations. Computational tasks still require multiprocessing or threading; otherwise, it will block the event loop.
Pros:
Cons:
Negative Case: Young developers decided to speed up the application using async/await, but asynchronously performed only computations and not network requests. The application did not speed up. Pros: got acquainted with the syntax. Cons: no benefits, code became more complex.
Positive Case: Asynchronously processed thousands of requests to an API. The server started serving more clients without increasing resources. Pros: performance significantly increased, architecture became simpler. Cons: the entry threshold for newbies increased.