Await in loop (ARCH2030)
هذا المحتوى غير متوفر بلغتك بعد.
Key python.async.await-in-loop · Severity info · Category async
Awaiting each element inside a loop body serializes independent async operations. With asyncio.gather they can run concurrently, cutting total latency.
Incorrect
Section titled “Incorrect”async def load_all(ids): results = [] for i in ids: results.append(await fetch(i)) # serial return resultsCorrect
Section titled “Correct”import asyncio
async def load_all(ids): return await asyncio.gather(*(fetch(i) for i in ids)) # concurrentCollect independent calls with asyncio.gather and await them concurrently in one pass.