plynch714
plynch714

Reputation: 43

How to asynchronously run functions within a for-loop in Python?

Hi I was wondering how to asynchronously call a function within a for-loop in Python, allowing the for-loop to execute more quickly. bar() in this case is a time intensive function, which is why I want the calls to it to be nonblocking. Here is what I want to refactor:

def bar(item):
    //manipulate item
    return newItem

newItems = []
for item in items:
    newItem = foo(item)
    newItems.append[newItem]

Here is what I've tried:

async def bar(item):
    //manipulate item
    return newItem

async def foo():
    newItems = [bar(item) for item in items]
    newItems = await asyncio.gather(*newItems)
    return newItems

newItems = asyncio.run(foo())

This doesn't seem to work as each function call still waits for the previous one to finish before starting. I would love tips on what I might be doing wrong. Thank you so much for any and all help!

Upvotes: 4

Views: 2005

Answers (1)

Artiom  Kozyrev
Artiom Kozyrev

Reputation: 3826

If your tasks are really async you can do it the following way:

import asyncio


async def bar(item: int) -> int:
    # manipulate item
    print("Started")
    await asyncio.sleep(5)
    print("Finished")
    return item ** 2


async def foo():
    items = range(1, 10)
    tasks = [bar(item) for item in items]
    new_items = await asyncio.gather(*tasks)
    return new_items

if __name__ == '__main__':
    results = asyncio.run(foo())
    print(results)

Upvotes: 3

Related Questions