gpt4 book ai didi

Python 多处理示例不起作用

转载 作者:太空狗 更新时间:2023-10-29 18:20:26 25 4
gpt4 key购买 nike

我正在尝试学习如何使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com