TIL: Concurrent API Calls With asyncio in Python
Processing audio files one at a time is slow. Each file means a network round trip to the API. asyncio runs multiple requests concurrently.
import asyncio
import aiohttp
async def transcribe(session, filepath):
data = open(filepath, 'rb').read()
async with session.post('https://api.example.com/v1/listen', data=data) as resp:
return await resp.json()
async def main():
files = ['file1.wav', 'file2.wav', 'file3.wav']
async with aiohttp.ClientSession() as session:
results = await asyncio.gather(
*(transcribe(session, f) for f in files)
)
print(results)
asyncio.run(main())
asyncio.gather runs all the coroutines concurrently. The total time is the slowest single request, not the sum of all requests.
Do I need aiohttp or can I use requests?
requests is synchronous and blocks. Use aiohttp or httpx for async HTTP in Python.
What if one request fails?
gather raises the first exception by default. Use return_exceptions=True to collect results and exceptions separately.