4

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!

1
  • You may need to utilize Python's multiprocessibng capability for what you are doing since, async is usually only useful in speeding things up if you have an I/O operation which needs to wait. seeSpeeding Up Python with Concurrency, Parallelism, and asyncio for further clarification. Commented Jul 14, 2022 at 20:59

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Perhaps my issues stem from my ignorance of what makes certain functions "really async" as I thought I could make any function asynchronous. Thank you for the reply.
@plynch714 well if my answer solves the question for async tasks, please mark it as solution. Actually if your tasks are not async, for CPU heave tasks use multiprocessing module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.