Async Python Programming
asyncio enables concurrent code execution for I/O-bound tasks.
Basic async/await
import asyncio
async def fetch_data(url):
await asyncio.sleep(1) # Simulate API call
return f"Data from {url}"
async def main():
results = await asyncio.gather(
fetch_data("url1"),
fetch_data("url2"),
)
print(results)
asyncio.run(main())
{# Provenance for articles generated from a source document. Rendered
here rather than stored in the body so it stays accurate and cannot
be edited away, and so the sealed draft stays purely generated. #}
Discussion 0
No comments yet. Be the first to start the discussion!
Leave a Comment