간단한 예제 : 두 개의 관련없는 HTTP 요청을 병렬로 만들어야합니다. 가장 간단한 방법은 무엇입니까? 나는 그것이 그렇게 될 것으로 기대 :파이썬의 코 루틴에서 병렬 비동기 IO
async def do_the_job():
with aiohttp.ClientSession() as session:
coro_1 = session.get('http://httpbin.org/get')
coro_2 = session.get('http://httpbin.org/ip')
return combine_responses(await coro_1, await coro_2)
즉, 내가 IO 작업을 시작하고 효과적으로 병렬로 실행할 수 있도록 그 결과를 기다려야합니다. 이것은 asyncio.gather
을 달성 할 수 있습니다
async def do_the_job():
with aiohttp.ClientSession() as session:
coro_1 = session.get('http://example.com/get')
coro_2 = session.get('http://example.org/tp')
return combine_responses(*(await asyncio.gather(coro_1, coro_2)))
다음, 좀 복잡한 의존성 구조를 갖고 싶어? 결과를 얻으 려 할 때 결과를 얻고 싶습니다. 여기에 별도 이벤트 루프에 의해 관리되는 코 루틴에서 별도의 작업을하게 asyncio.ensure_future
하는 데 도움이 :
async def do_the_job():
with aiohttp.ClientSession() as session:
fut_1 = asyncio.ensure_future(session.get('http://httpbin.org/ip'))
coro_2 = session.get('http://httpbin.org/get')
coro_3 = session.post('http://httpbin.org/post', data=(await coro_2)
coro_3_result = await coro_3
return combine_responses(await fut_1, coro_3_result)
이 사실 병렬 내 논리 흐름에서 코 루틴과 IO 비 차단 달성하기 위해, 즉, 내가 사용해야 하나 asyncio.ensure_future
또는 asyncio.gather
(실제로는 asyncio.ensure_future
)? 덜 "장황한"방법이 있습니까?
일반적으로 개발자는 coroutines가 별도의 작업이되어야한다고 생각하고 최적의 성능을 얻기 위해 앞서 언급 한 기능을 사용해야합니까?
이벤트 루프에서 여러 작업을 수행하지 않고 동시 루틴을 사용할 수 있습니까?
실제 상황에서 이벤트 루프 작업이 얼마나 무겁습니까? 확실히 OS 쓰레드 나 프로세스보다 "가볍다". 그러한 작업의 가능한 최소 수를 위해 어느 정도까지 노력해야합니까?
주로 내 질문에 대한 답변입니다. 'asyncio.gather'와 코 루틴 함수 또는'asyncio.ensure_future'를 사용한 연결은 코 루틴과 병렬로 IO를 수행하는 표준 방법입니다. 요점으로, 필자는 예제 코드가 잘못해서 aiohttp를 사용한다는 것을 이해했다. –