2016-06-18 13 views
5

Sto tentando di integrare una barra di avanzamento tqdm per monitorare le richieste POST generate con aiohttp in Python 3.5. Ho una barra di avanzamento di lavoro ma non riesco a raccogliere i risultati usando as_completed(). Puntatori ricevuti con gratitudine.barra di avanzamento asyncio aiohttp con tqdm

esempi che ho trovato suggerire utilizzando il seguente modello, che è incompatibile con Python 3.5 async def definizioni:

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

di lavoro (anche se redatte in parti) di codice asincrono, senza la barra di avanzamento:

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) 

Questo è il mio tentativo non riuscito a modificare 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) 

risposta

10

await f restituisce una risposta singola. Perché passare un Future già completato a asyncio.gather(f) non è chiaro.

Prova:

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

Python 3.6 implementa PEP 530 -- Asynchronous Comprehensions:

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

funziona Interni async def funzioni ora.

+0

Grazie mille:) –

+0

@ j-f-sebastien Bella! Grazie per avermi fatto sapere –

Problemi correlati