0% found this document useful (0 votes)
3 views2 pages

Python Async

Asynchronous programming in Python using asyncio allows for concurrent handling of multiple I/O-bound tasks, improving efficiency by not blocking during waits. The document contrasts synchronous and asynchronous functions, demonstrating how async functions can run concurrently and reduce total execution time. It also covers core concepts like coroutines, the use of await, and the asyncio.gather method for managing multiple tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Python Async

Asynchronous programming in Python using asyncio allows for concurrent handling of multiple I/O-bound tasks, improving efficiency by not blocking during waits. The document contrasts synchronous and asynchronous functions, demonstrating how async functions can run concurrently and reduce total execution time. It also covers core concepts like coroutines, the use of await, and the asyncio.gather method for managing multiple tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Async

Asynchronous Programming with asyncio

Why Async?

Asynchronous programming allows Python to handle multiple I/O-bound tasks concurrently without blocking. Instead of waiting idle
for a network response or file read, the program switches to other tasks. Ideal for web servers, APIs, and any I/O-heavy workloads.

Sync vs Async
# SYNC - blocks for 2 seconds total
def fetch_sync():
[Link](1) # wait
[Link](1) # wait again
# Total: 2 seconds

# ASYNC - runs concurrently


async def fetch_async():
await [Link](
[Link](1),
[Link](1),
)
# Total: ~1 second

Core Concepts
async def my_coroutine():
# async def → defines a coroutine
# await → suspends until result ready
# gather() → run multiple coroutines
result = await some_io_operation()
return result

# Run the coroutine


[Link](my_coroutine())

[Link]
async def task(name, delay):
await [Link](delay)
return f'{name} done'

async def main():


results = await [Link](
task('A', 1),
task('B', 2),
task('C', 1),
)
# All run concurrently
# Total time: ~2s not 4s
print(results)

async with httpx


import httpx

async def fetch_url(url: str):


async with [Link]() as client:
response = await [Link](url)
return [Link]()

async def main():


urls = ['[Link]
'[Link]
results = await [Link](
*[fetch_url(u) for u in urls]
)

Python Reference Sheet — Python Async | Generated 2026

You might also like