Python多处理示例不起作用 [英] Python multiprocessing example not working
问题描述
我正在尝试学习如何使用multiprocessing
,但是我无法使其正常工作.这是文档
I am trying to learn how to use multiprocessing
but I can't get it to work. Here is the code right out of the 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'
但是我得到了
>
没有错误或其他消息,它只是坐在那里,它从Windows 7计算机上已保存的.py文件中的IDLE
中运行,该Windows 7计算机具有32位版本的python 2.7
no errors or other messages, it just sits there, It is running in IDLE
from a saved .py file on a Windows 7 machine with the 32-bit version of Python 2.7
推荐答案
我的猜测是您正在使用IDLE尝试运行此脚本.不幸的是,此示例无法在IDLE中正确运行.请注意文档开头的评论:
My guess is that you are using IDLE to try to run this script. Unfortunately, this example will not run correctly in IDLE. Note the comment at the beginning of the docs:
请注意,此软件包中的功能需要主要 该模块可由儿童导入.这在编程中有介绍 准则,但是这里值得指出.这意味着一些 例子,例如multiprocessing.Pool例子将无法在 交互式解释器.
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.
即使您将脚本作为具有IDLE的文件运行(通常使用F5完成),__main__
模块也不能由IDLE中的子级导入.
The __main__
module is not importable by children in IDLE, even if you run the script as a file with IDLE (which is commonly done with F5).
这篇关于Python多处理示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!