python 除非程序崩溃,否则子 multiprocessing.Process 没有打印输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/367053/
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-11-03 19:58:05  来源:igfitidea点击:

No print output from child multiprocessing.Process unless the program crashes

pythonmultithreadingiomultiprocessing

提问by orip

I am having trouble with the Python multiprocessing module. I am using the Processclass to spawn a new process in order to utilize my second core. This second process loads a bunch of data into RAM and then waits patiently instead of consuming.

我在使用 Python 多处理模块时遇到了问题。我正在使用Process该类生成一个新进程以利用我的第二个核心。第二个进程将一堆数据加载到 RAM 中,然后耐心等待而不是消耗。

I wanted to see what that process printed with the printcommand, however, I do not see anything that it prints. I only see what the parent process prints. Now this makes sense to me since they live in two different process. The second process doesn't spawn its own shell/standard output window, nor is its output sent to the parent. Yet when this process crashs, it prints everything that my script told it to print, plus the stack trace and error.

我想看看这个进程用print命令打印了什么,但是,我没有看到它打印的任何东西。我只看到父进程打印的内容。现在这对我来说很有意义,因为他们生活在两个不同的过程中。第二个进程不会产生自己的 shell/标准输出窗口,也不会将其输出发送到父进程。然而,当这个进程崩溃时,它会打印我的脚本告诉它打印的所有内容,以及堆栈跟踪和错误。

I am wondering if there is a simple way to send the child process's print output to the first process, or have it spawn a shell/standard output so that I may debug it. I know I could create a multiprocessing.Queuededicated to transmitting prints to the parent so that it may print these to standard output, but I do not feel like doing this if a simpler solution exists.

我想知道是否有一种简单的方法可以将子进程的打印输出发送到第一个进程,或者让它产生一个 shell/标准输出,以便我可以调试它。我知道我可以创建一个multiprocessing.Queue专门用于将打印件传输给父级的文件,以便它可以将这些打印件打印到标准输出,但是如果存在更简单的解决方案,我不想这样做。

回答by orip

Have you tried flushing stdout?

你试过刷新标准输出吗?

import sys
print "foo"
sys.stdout.flush()