rajan sthapit
rajan sthapit

Reputation: 4364

Asyncio not running things in parallel

I am running this asyncio example

import asyncio
from aiohttp import ClientSession

async def fetch(url, session, index):
    async with session.get(url) as response:
        print("Before " + str(index))
        buffer = await response.read()
        print("After " + str(index))

async def run(r):
    url = "http://google.com"
    tasks = []

    # Fetch all responses within one Client session,
    # keep connection alive for all requests.
    async with ClientSession() as session:
        for i in range(r):
            task = asyncio.ensure_future(fetch(url, session, i))
            tasks.append(task)

        responses = await asyncio.gather(*tasks)
        # you now have all response bodies in this variable
        print(responses)

def print_responses(result):
    print(result)

loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(4))
loop.run_until_complete(future)

The results are something like this

Before 1
After 1
Before 3
After 3
Before 2
After 2
Before 0
After 0

This looks like the things are running serially. The fetch part itself takes some milliseconds. I ran it several times, but the results are not intermingled. Any suggestions?

Upvotes: 1

Views: 1699

Answers (1)

Michal Charemza
Michal Charemza

Reputation: 27062

As mentioned in comments, it's likely that by the time response.read() is called, there is nothing remaining to download, the coroutine isn't suspended, and print("After " + str(index)) is then immediately called.

To see more of an intermingling, you can choose a slower URL. For example for me

url = "http://speedtest.tele2.net/100MB.zip"

outputs

Before 0
Before 1
Before 3
Before 2
After 0
After 1
After 2
After 3

Upvotes: 2

Related Questions