简体   繁体   中英

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM