작성
·
212
0
비동기 처리 관련 강의 들으며 여러가지로 고민이 많아 질문을 합니다.
아래와 같이 여러개의 값을 입력 받아 비동기 처리하는 함수를 작성했습니다.
여러개의 값을 받아 비동기 처리할 때 3개씩 묶어 처리할 수 있도록 하고 싶어서 문의 글을 올립니다.
a,b,c 비동기 처리 > d,e,f 비동기 처리 될 수 있도록 하는 방법이 있을까요?
from multiprocessing import Pool
def multiPool(a, b, c, d, e, f):
values = ( a, b, c, d, e, f )
with Pool() as pool:
res = pool.starmap(fun, values)
return a, b, c, d, e, f
답변 1
1
비동기 처리를 하셔도 값을 기다리시면 됩니다.
만약 순차적으로 값을 반환해서 프로세스를 처리하신다면 처리 완료 되는대로 반환하시고
비동기여도 값을 모두 대기한 후 하나의 데이터로 묶어서 반환하시려면
async gather 또는 asyncio wait 등으로 묶어서 반환 가능합니다.
아래는 공식 설명입니다.
You need to use asyncio.gather()
instead of asyncio.wait()
, in which case you won't need to call result()
on the returned values. (Once the previous point is implemented, you could use asyncio.wait()
as you tried, and call result()
on the tasks, but asyncio.gather()
is designed so you don't have to do that.)