No print output from child multiprocessing.Process unless the program crashes(子 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 没有打印输出,除非程序崩溃
基础教程推荐
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
