Python 多处理池,加入;不等下去了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20914828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:32:28  来源:igfitidea点击:

Python multiprocessing pool, join; not waiting to go on?

pythonpython-2.7multiprocessing

提问by panavia

(1) I'm trying to use pool.mapfollowed by pool.join(), but python doesn't seem to be waiting for pool.mapto finish before going on past the pool.join(). Here's a simple example of what I've tried:

(1) 我正在尝试使用pool.map后跟pool.join(),但 python 似乎并没有pool.map在继续之前等待完成pool.join()。这是我尝试过的简单示例:

from multiprocessing import Pool

foo = {1: []}

def f(x):
    foo[1].append(x)
    print foo

def main():
    pool = Pool()
    pool.map(f, range(100))
    pool.close()
    pool.join()
    print foo

if __name__ == '__main__':
    main()

The printed output is just {1: []}, as if python just ignored the joincommand and ran print foobefore it had a chance to run f. The intended result is that foois {1:[0,1,...,99]}, and using the ordinary built-in python mapgives this result. Why is the pooled version printing {1: []}, and how can I change my code to make it print the intended result?

打印的输出只是{1: []},好像 python 只是忽略了join命令并print foo在它有机会运行之前运行了f。预期的结果是,foo{1:[0,1,...,99]},使用普通的内置Pythonmap给出了这样的结果。为什么要打印合并版本{1: []},如何更改我的代码以使其打印预期结果?

(2) Ideally I'd also like to define fooas a local variable in main()and pass it to f, but doing this by making foothe first argument of fand using

(2) 理想情况下,我还想foo在 in 中定义为局部变量main()并将其传递给f,但是通过使foo第一个参数f和使用

pool.map(functools.partial(f, foo), range(100))

pool.map(functools.partial(f, foo), range(100))

produces the same output. (and possibly also has the problem that each process now has its own copy of foo?) Though again, it works using the normal mapinstead.

产生相同的输出。(并且可能还有问题,每个进程现在都有自己的副本foo?)尽管如此,它还是使用正常的map

采纳答案by smeso

This is not the correct way to use map.

这不是正确的使用方法map

  1. Using a global variable that way is absolutely wrong. Processes do not share the same memory (normally) so every fwill have his own copy of foo. To share a variable between different processes you should use a Manager
  2. Function passed to mapare, usually, expected to return a value.
  1. 以这种方式使用全局变量是绝对错误的。进程不共享相同的内存(通常),因此每个进程f都有自己的foo. 要在不同的进程之间共享变量,您应该使用Manager
  2. 传递给的函数map通常会返回一个值。

I suggest you to read some documentation.

我建议你阅读一些文档

However here is a dummy example of how you could implement it:

但是,这里有一个关于如何实现它的虚拟示例:

from multiprocessing import Pool

foo = {1: []}

def f(x):
    return x

def main():
    pool = Pool()
    foo[1] = pool.map(f, range(100))
    pool.close()
    pool.join()
    print foo

if __name__ == '__main__':
    main()

You may also do something like pool.map(functools.partial(f, foo), range(100))where foois a Manager.

您也可以执行类似pool.map(functools.partial(f, foo), range(100))where foois a 之类的操作Manager