Python 多处理示例不起作用

标签 python multiprocessing

我正在尝试学习如何使用 multiprocessing,但我无法让它工作。这是 documentation 中的代码

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

它应该输出

'hello bob'

但是我得到了

>

没有错误或其他消息,它只是坐在那里,它正在 IDLE 中运行,来自装有 32 位版本 Python 2.7 的 Windows 7 机器上保存的 .py 文件

最佳答案

我的猜测是您正在使用 IDLE 来尝试运行此脚本。不幸的是,这个例子在 IDLE 下不能正确运行。注意the docs开头的注释:

Note Functionality within this package requires that the main module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter.

__main__ 模块在 IDLE 模式下不能被子进程导入,即使您使用 IDLE 将脚本作为文件运行(通常使用 F5 完成)。

关于Python 多处理示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21198857/

相关文章:

python - 构建 Cython-HIDapi Python 2.7

python - 是否可以在多个核上运行依赖于前一个任务的程序?

python - 关于keras.utils.Sequence的澄清

python - Cassandra 多处理无法 pickle _thread.lock 对象

c++ - 关于一个编译器同时执行多个任务

Python multiprocessing.Pool 每个变量的新进程

python - Selenium 不能在服务器上工作,因为它在本地机器上工作

javascript - scrapy-splash 无法呈现此页面 - 未呈现动态内容?

python - 在pyqt中显示接收到的图像

python - 如果我使用多处理,函数返回值类型错误