Basic async functions and await expressions
The async and await keywords are the foundation of asynchronous programming in Python. Functions defined with async def are coroutine functions that return coroutine objects when called. The await keyword is used to suspend execution of a coroutine until the awaited operation completes, allowing other tasks to run during the wait. Await can only be used inside async functions. Coroutines can be scheduled to run on an event loop using asyncio.run() or by creating tasks. The asyncio.sleep() function is the asynchronous equivalent of time.sleep() and is commonly used for demonstrations. Understanding the basic async/await syntax is the first step toward writing asynchronous code. This paradigm allows writing code that looks synchronous but operates asynchronously, making it easier to reason about while still benefiting from concurrency for I/O-bound operations.