Io uso asyncio
e bello aiohttp
. L'idea principale è che faccio richiesta al server (restituisce i collegamenti) e poi voglio scaricare i file da tutti i collegamenti in parallelo (qualcosa come in un example).Perché ottengo un errore "Task è stato distrutto ma è in sospeso" in Python asyncio?
Codice:
import aiohttp
import asyncio
@asyncio.coroutine
def downloader(file):
print('Download', file['title'])
yield from asyncio.sleep(1.0) # some actions to download
print('OK', file['title'])
def run():
r = yield from aiohttp.request('get', 'my_url.com', True))
raw = yield from r.json()
tasks = []
for file in raw['files']:
tasks.append(asyncio.async(downloader(file)))
asyncio.wait(tasks)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
Ma, quando provo a farlo funzionare, ho molti "Scarica ..." uscite e
Task was destroyed but it is pending!
E nulla di 'OK + filename'.
Come posso risolvere il problema?
Mille grazie per la bella risposta – tim