有没有一种简单的 “类似tqdm” 的方法来使for循环运行多进程?

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

Is there an easy "tqdm like" way to make a for loop to run multiprocess?

问题

我有一个在Python中的for循环,我想在多个进程中运行。我知道我可以使用multiprocessing模块来实现这一点,但我想知道是否有一个库可以让我用类似于tqdm的简单语法来实现这个。这是我想要实现的:

for i in some_multiprocess_library(range(100), n_processes=4):
    some_func(i)
英文:

I have a for loop in Python that I want to run in multiple processes. I know I can use the multiprocessing module to achieve this, but I was wondering if there is a library that allows me to do this with a simple syntax similar to how tqdm works. Here is what I want to achieve:

for i in some_multiprocess_library(range(100), n_processes=4):
    some_func(i)


</details>


# 答案1
**得分**: 1

```python
from tqdm.contrib.concurrent import process_map

for i in process_map(range(1_000)):
   some_func(i)

同样的建议也适用于Python的multiprocessing模块。

英文:

You can use:

from tqdm.contrib.concurrent import process_map

for i in process_map(range(1_000)):
   some_func(i)

The same recommendations from the Python multiprocessing modules apply.

huangapple
  • 本文由 发表于 2023年3月31日 20:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898501.html
匿名

发表评论

匿名网友

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

确定