the part of asyncio that checks if the response is ready

49 Views Asked by At

In the asynchronous code below, what is the thing that checks if the response is ready, for each task to resume the execution ?

I understand that the 50 requests are all sent one after another at async with session.get(url).

But how does it then checks that responses are ready ?

Is it the event loop, that periodically checks ?

Is it asyncio that "yields" control from one task to another to see if a response is ready ?

import aiohttp
import asyncio
import time

async def fetch_page(url):
    page_start = time.time()
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            print(f'Page took {time.time() - page_start}')
            return response.status

loop = asyncio.get_event_loop()
tasks = [fetch_page('http://google.com') for i in range(50)]
start = time.time()
loop.run_until_complete(asyncio.gather(*tasks))
print(f'All took {time.time() - start}')
0

There are 0 best solutions below