Not seeing speed up with Python Async IO

474 Views Asked by At

I have an IO operation (POST request for each Pandas row) which I'm trying to speed up using Async IO. An alternative minimal example can be found below. I want to understand why the 1st sample doesn't run in parallel and 2nd sample is faster.

1st Sample:

from time import sleep
import asyncio
import nest_asyncio
nest_asyncio.apply()


async def add(x: int, y: int, delay: int):
  sleep(delay) #await asyncio.sleep(delay)
  return x+y
  

async def get_results():
  inputs = [(2,3,9), (4,5,7), (6,7,5), (8,9,3)]
  
  cors = [add(x,y,z) for x,y,z in inputs]
  results = asyncio.gather(*cors)
    
  print(await results)

asyncio.run(get_results())

# This takes ~24s

2nd Sample:

from time import sleep
import asyncio
import nest_asyncio
nest_asyncio.apply()


async def add(x: int, y: int, delay: int):
  await asyncio.sleep(delay)
  return x+y
  

async def get_results():
  inputs = [(2,3,9), (4,5,7), (6,7,5), (8,9,3)]
  
  cors = [add(x,y,z) for x,y,z in inputs]
  results = asyncio.gather(*cors)
    
  print(await results)

asyncio.run(get_results())

# This takes ~9s as expected

In my case the sleep can be replace with requests.post()

3

There are 3 best solutions below

4
On BEST ANSWER

requests isn't async aware, so trying to use it in an async function doesn't make anything faster.

You will need an async aware HTTP library such as httpx or aiohttp to make HTTP requests that don't block async functions.

Similarly, in the first example, you're using a non-async-aware function, time.sleep, which blocks the async loop.

Also, a non-IO, Python-native-code operation (an addition) will not be sped up by asyncio.

(Recall that async doesn't mean that the functions would be run in parallel, quite the contrary.)

0
On

As soon as your code hits sleep.delay it will block all other co-routines from executing in the event loop. All function have to be async compatible

0
On

Because the time.sleep is not async. Thread will be blocking and waiting when running to the code time.sleep.

However await asyncio.sleep is async. Thread will be free to execute other code when running to the code await asyncio.sleep.

sync just like you must order a food and waiting it completion. Then you can oder next food.

async means you can order all food in a time and waiting them completion.