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

No print output from child multiprocessing.Process unless the program crashes(子 multiprocessing.Process 没有打印输出,除非程序崩溃)

本文介绍了子 multiprocessing.Process 没有打印输出,除非程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble with the Python multiprocessing module. I am using the Process class 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.

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

I wanted to see what that process printed with the print command, 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.

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

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.Queue dedicated 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.

推荐答案

你试过刷新标准输出吗?

Have you tried flushing stdout?

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

这篇关于子 multiprocessing.Process 没有打印输出,除非程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:子 multiprocessing.Process 没有打印输出,除非程序崩溃

基础教程推荐