Backend2024년 8월 4일1분 읽기

Python 비동기 프로그래밍 — asyncio 실전 가이드

YS
YoungSam
조회 1560

코루틴 기본

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는 개별 작업을 백그라운드로 실행합니다.

PythonasyncioAsync

댓글 0

아직 댓글이 없습니다.