-1

I have this code for scraping printers webpage with asyncio:

def get_toner(url,loop):
    loop.run_in_executor(executor,scraper,url)

def scraper(url):
    
#function for scraping return % of toner

loop = asyncio.get_event_loop()

def main():
    for stampante in lista_totale:
        get_toner(stampante,loop=loop)
        
    lista_verificati.append(loop.run_until_complete(asyncio.gather(*asyncio.all_tasks(loop))))
    print (lista_verificati)

if __name__ == "__main__":
    main()

my intent is to create a list to work with at the end of the loop. But I can't print the complete list, the code only prints the empty list immediately after the function starts.

i have read this:

asyncio-collecting-results-from-an-async-function-in-an-executor

return-results-from-asyncio-get-event-loop

return-a-list-from-asyncio-function

Gian76
  • 81
  • 6
  • 1
    I see no async functions in your example, consider not to use ```asyncio``` if you have no async functions. Or use e.g. aiohttp - it hase async http async client. Do you need some example to build the code? – Artiom Kozyrev Mar 08 '21 at 10:22

1 Answers1

-1

I found the solution with loop.shutdown_asyncgens():

try:
    loop.run_until_complete(asyncio.gather(*asyncio.all_tasks(loop)))
finally:
    loop.run_until_complete(loop.shutdown_asyncgens())
    executor.shutdown(wait=True)
    loop.close()
    print (lista_verificati)
Gian76
  • 81
  • 6