2016-06-18 20 views
5

Ich versuche eine tqdm Fortschrittsanzeige zu integrieren, um POST-Anfragen zu überwachen, die mit aiohttp in Python 3.5 generiert wurden. Ich habe einen Fortschrittsbalken, kann aber keine Ergebnisse mit as_completed() sammeln. Zeiger dankbar erhalten.asyncio aiohttp Fortschrittsbalken mit tqdm

Beispiele ich gefunden habe, deuten darauf hin, das folgende Muster verwenden, die mit Python 3.5 async def Definitionen unvereinbar ist:

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

Arbeiten (wenn auch anonymisiert in Teilen) Asynchron-Code ohne die Fortschrittsbalken:

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) 

Das ist mein erfolgloser Versuch bei loop() ändern:

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) 

Antwort

10

await f gibt eine einzelne Antwort zurück. Warum würdest du einen schon fertiggestellten Future an asyncio.gather(f) übergeben, ist unklar.

Versuchen:

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

Python 3.6 implementiert PEP 530 -- Asynchronous Comprehensions:

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

Es funktioniert innerhalb async def Funktionen jetzt.

+0

Vielen Dank:) –

+0

@ j-f-sebastien Schön! Danke für die Information –