2016-06-18 5 views
5

tqdm 진행률 표시 줄을 통합하여 파이썬 3.5에서 aiohttp으로 생성 된 POST 요청을 모니터하려고합니다. 진행률 표시 줄이 있지만 as_completed()을 사용하여 결과를 수집 할 수 없습니다. 포인터는 감사하게 받았다. 내가 찾은asyncio aiohttp tqdm의 진행 막대

예는 파이썬과 호환되지 않는 다음과 같은 패턴을 사용하는 것이 좋습니다 3.5 async def 정의 :

for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(coros)): 
    yield from f 

작업 (이기는하지만 부분으로 편집 됨) 진행 표시 줄이없는 비동기 코드 :

def async_classify(records): 

    async def fetch(session, name, sequence): 
     url = 'https://app.example.com/api/v0/search' 
     payload = {'sequence': str(sequence)} 
     async with session.post(url, data=payload) as response: 
      return name, await response.json() 

    async def loop(): 
     auth = aiohttp.BasicAuth(api_key) 
     conn = aiohttp.TCPConnector(limit=100) 
     with aiohttp.ClientSession(auth=auth, connector=conn) as session: 
      tasks = [fetch(session, record.id, record.seq) for record in records] 
      responses = await asyncio.gather(*tasks)  
     return OrderedDict(responses) 

이것은 내 수정 시도에 실패했습니다. loop() :

async def loop(): 
    auth = aiohttp.BasicAuth(api_key) 
    conn = aiohttp.TCPConnector(limit=100) 
    with aiohttp.ClientSession(auth=auth, connector=conn) as session: 
     tasks = [fetch(session, record.id, record.seq) for record in records] 
     for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks)): 
      await f 
     responses = await asyncio.gather(f) 
     print(responses) 

답변

10

await f단일 응답을 반환합니다. 왜 이미 완성 된 Futureasyncio.gather(f)에 전달 하시겠습니까?

시도 :


responses = [] 
for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks)): 
    responses.append(await f) 
파이썬 3.6 구현 PEP 530 -- Asynchronous Comprehensions :

responses = [await f 
      for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks))] 

그것은 지금 내 async def 기능을 사용할 수 있습니다.

+0

대단히 감사합니다.) –

+0

@ j-f-sebastien 러블리! 알려 줘서 고마워 –