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

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

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."

  1. async def dumb_add(i,j):
  2. print("adding",i,"+",j)
  3. await asyncio.sleep(random.randint(0,3))
  4. print(i,"+",j,"=",(i+j))
  5. async def main():
  6. for i in range(0,2):
  7. for j in range(0,2):
  8. await dumb_add(i,j)
  9. print('main done')
  10. asyncio.create_task(main())

Results:

  1. adding 0 + 0
  2. 0 + 0 = 0
  3. adding 0 + 1
  4. 0 + 1 = 1
  5. adding 1 + 0
  6. 1 + 0 = 1
  7. adding 1 + 1
  8. 1 + 1 = 2
  9. 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.

  1. async def dumb_add(i,j):
  2. print("adding",i,"+",j)
  3. await asyncio.sleep(random.randint(0,3))
  4. print(i,"+",j,"=",(i+j))
  5. async def main():
  6. for i in range(0,2):
  7. for j in range(0,2):
  8. await dumb_add(i,j)
  9. print('main done')
  10. asyncio.create_task(main())

Results:

  1. adding 0 + 0
  2. 0 + 0 = 0
  3. adding 0 + 1
  4. 0 + 1 = 1
  5. adding 1 + 0
  6. 1 + 0 = 1
  7. adding 1 + 1
  8. 1 + 1 = 2
  9. main done

答案1

得分: 0

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

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

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

类似于以下内容:

  1. async def dumb_add(i, j):
  2. print("添加", i, "+", j)
  3. await asyncio.sleep(random.randint(0, 3))
  4. print(i, "+", j, "=", (i + j))
  5. async def main():
  6. tasks = []
  7. for i in range(0, 2):
  8. for j in range(0, 2):
  9. tasks.append(dumb_add(i, j))
  10. await asyncio.gather(*tasks)
  11. print('主程序完成')
  12. 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:

  1. async def dumb_add(i,j):
  2. print("adding",i,"+",j)
  3. await asyncio.sleep(random.randint(0,3))
  4. print(i,"+",j,"=",(i+j))
  5. async def main():
  6. tasks = []
  7. for i in range(0,2):
  8. for j in range(0,2):
  9. tasks.append(dumb_add(i,j))
  10. await asyncio.gather(*tasks)
  11. print('main done')
  12. 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:

确定