Good Code
The good version collects independent async calls and awaits them together.
Lesson 10
Await asynchronous work and gather independent calls intentionally so coroutines do not leak out unfinished.
import asyncio
async def fetch_profiles(user_ids, api):
tasks = [api.fetch_profile(user_id) for user_id in user_ids]
return await asyncio.gather(*tasks)async def fetch_profiles(user_ids, api):
profiles = []
for user_id in user_ids:
profiles.append(api.fetch_profile(user_id))
return profilesThe good version collects independent async calls and awaits them together.
The bad version returns coroutine objects instead of profile data. The caller may see confusing values, and the intended I/O may never complete.