Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python event loop gets finished before all task finishes

I’m using pythons asyncio module for running concurrent network requests.

async def fetch(url, max_pages):
    async with aiohttp.ClientSession() as session:
        tasks = [get_html(session, f"{url}/{i}/") for i in range(1, max_pages + 1)]
        await asyncio.gather(*tasks) #this function exits before all respose arrives.

loop = asyncio.get_event_loop()
loop.run_until_complete(fetch(url, max_pages))

What is wrong in my code?

I’m expecting to finish all tasks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Well I’m not sure without looking at more code. But I thing you can try using,

asyncio.run(fetch(url, max_pages))

This might solve your problem.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading