2016-06-18 6 views
5

tqdm進捗バーを統合して、aiohttpで生成されたPOST要求をPython 3.5で監視しようとしています。私は進行中のバーを持っていますが、as_completed()を使って結果を集めるように見えません。ポインタは感謝して受け取りました。私が見つけたasyncio aiohttp tqdmの進行状況

例としては、Pythonと互換性がありません。次のパターンを、使用することをお勧め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) 

のPython 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ラブリー!知らせてくれてありがとうございます –

関連する問題