【问题标题】:Python multiprocessing not printing example code to stdoutPython多处理不将示例代码打印到标准输出
【发布时间】:2020-08-17 04:56:27
【问题描述】:

我完全从 Python multiprocessing Process documentation 复制了这段代码。

from multiprocessing import Process

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

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

我希望当它在 IDLE 中运行时,我会看到“hello bob”回来。相反,我什么也得不到。检查 p 变量会显示正常的退出代码。

============= RESTART: C:/Users/Hoofran/Desktop/DemoMultiProcess.py ============
>>> p
<Process name='Process-1' pid=17268 parent=18116 stopped exitcode=0>
>>> 

这就是多处理应该如何工作,抑制标准输出吗?如果是这样,为什么文档没有提到这一点?我在这里错过了什么?

【问题讨论】:

  • 输出看起来像是从某个 IDE 或笔记本上运行的,我不知道?请edit向我们展示这方面的详细信息。
  • @tripleee OP 说这是从 IDLE 运行的,一个 python IDE
  • 代码正在运行,in online compiler。问题出在 IDE 中。

标签: python multiprocessing python-multiprocessing python-3.8


【解决方案1】:

这似乎是 IDLE 特有的问题。跨多台机器复制问题。在 IDLE python 3.8.3 中运行不会打印任何行,但从命令行运行会按预期打印。

PS C:\Users\ryan.mcgrath\Desktop> cat .\simpleMultiprocessing.py
from multiprocessing import Process

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

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


PS C:\Users\ryan.mcgrath\Desktop> python .\simpleMultiprocessing.py
hello bob
PS C:\Users\ryan.mcgrath\Desktop> python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 2016-06-26
    相关资源
    最近更新 更多