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!