Compare the performance of Async Generators vs. `asyncio.gather` for a sequence of 1000 network requests.
Python interview question for Advanced practice.
Answer
asyncio.gather is faster for total execution time because it starts all 1000 requests concurrently. However, it requires storing all 1000 results in memory at once. An async generator (if used with a semaphore or task queue) is more memory-efficient and provides the first result much faster (lower 'time to first byte'). Without extra logic, a plain async for loop over a generator is sequential and would be much slower than gather.
Explanation
gather runs tasks concurrently, while a simple async generator might run them sequentially unless combined with tasks.