import asyncio
async def fetch_data(url):
print(f"Fetching data from {url}")
await asyncio.sleep(2)
print(f"Data received from {url}")
async def main():
tasks = [
asyncio.create_task(fetch_data("https://api.example.com/data1")),
asyncio.create_task(fetch_data("https://api.example.com/data2")),
asyncio.create_task(fetch_data("https://api.example.com/data3")),
]
await asyncio.gather(*tasks)
asyncio.run(main())
网友回复