Async programming helps your program run multiple things at the same time without waiting around. Python uses async
and await
keywords to make this easy.
It lets your code pause and wait for tasks (like network calls) without freezing the whole program. This is great for speeding up I/O-bound tasks.
This example defines a simple async function that "waits" for 1 second before printing.
async def
: Declares an async function.await
: Pauses until the awaited task finishes.asyncio.run()
: Runs the main async function.Here, two async tasks run “simultaneously” with asyncio.gather()
.