코루틴 기본
import asyncio
async def fetch_data(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
async def main():
results = await asyncio.gather(
fetch_data("https://api1.com"),
fetch_data("https://api2.com"),
)
asyncio.run(main())asyncio.gather vs asyncio.create_task
gather는 여러 코루틴을 동시 실행하고 결과를 모아줍니다. create_task는 개별 작업을 백그라운드로 실행합니다.
댓글 0