是不是可以让Python中的嵌套循环异步运行?

huangapple go评论48阅读模式
英文:

Is it possible to make a nested loop run asynchronously in python?

问题

以下是您要翻译的内容:

"I was trying to run a cosine similarity code to check if two strings are similar inside my list of strings to make the list containing unique strings only to remove sentences that are similar. I took one string and compared it with every other string in the list. The method I implemented is O(n^2) and will take a month minimum to finish for all my strings. I was thinking if I could run the nested loop tasks in parallel to reduce the time using asyncio.

So I tried something very similar to this but it doesn't work asynchronously. Kindly guide me a little bit. thank you."

async def dumb_add(i,j):
    print("adding",i,"+",j)
    await asyncio.sleep(random.randint(0,3))
    print(i,"+",j,"=",(i+j))

async def main():
    for i in range(0,2):
        for j in range(0,2):
            await dumb_add(i,j)
    print('main done')

asyncio.create_task(main())

Results:

adding 0 + 0
0 + 0 = 0
adding 0 + 1
0 + 1 = 1
adding 1 + 0
1 + 0 = 1
adding 1 + 1
1 + 1 = 2
main done
英文:

I was trying to run a cosine similarity code to check if two strings are similar inside my list of strings to make the list containing unique strings only to remove sentences that are similar. I took one string and compared it with every other string in the list. The method I implemented is O(n^2) and will take a month minimum to finish for all my strings. I was thinking if I could run the nested loop tasks in parallel to reduce the time using asyncio.

So I tried something very similar to this but it doesn't work asynchronously. Kindly guide me a little bit. thank you.

async def dumb_add(i,j):
    print("adding",i,"+",j)
    await asyncio.sleep(random.randint(0,3))
    print(i,"+",j,"=",(i+j))

async def main():
    for i in range(0,2):
        for j in range(0,2):
            await dumb_add(i,j)
    print('main done')

asyncio.create_task(main())

Results:

adding 0 + 0
0 + 0 = 0
adding 0 + 1
0 + 1 = 1
adding 1 + 0
1 + 0 = 1
adding 1 + 1
1 + 1 = 2
main done

答案1

得分: 0

代码部分不需要翻译,以下是翻译好的内容:

不会并行运行,因为 "await" 关键字使协程在继续下一个之前等待每个 "dumb_add" 调用完成。因此,调用是顺序执行而不是同时进行。

如果要并行运行 "dumb_add" 函数,应该使用 asyncio.gather()。这样,你可以创建一个可以并行执行的协程列表。

类似于以下内容:

async def dumb_add(i, j):
    print("添加", i, "+", j)
    await asyncio.sleep(random.randint(0, 3))
    print(i, "+", j, "=", (i + j))

async def main():
    tasks = []
    for i in range(0, 2):
        for j in range(0, 2):
            tasks.append(dumb_add(i, j))
    await asyncio.gather(*tasks)
    print('主程序完成')

asyncio.run(main())
英文:

It is not running in parallel because the "await" keyword
is causing the co-routine to wait for each "dumb_add" call to finish, before moving on to the next one.
Therefore, the calls run sequentially rather than concurrently.

If you want to run your "dumb_add" function in parallel, you should use asyncio.gather().
In this way, you can create a list of routines that can be executed in parallel.

Something like this:

async def dumb_add(i,j):
    print("adding",i,"+",j)
    await asyncio.sleep(random.randint(0,3))
    print(i,"+",j,"=",(i+j))

async def main():
    tasks = []
    for i in range(0,2):
        for j in range(0,2):
            tasks.append(dumb_add(i,j))
    await asyncio.gather(*tasks)
    print('main done')

asyncio.run(main())

huangapple
  • 本文由 发表于 2023年2月19日 16:59:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定