問題描述
我在使用 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 沒有打印輸出,除非程序崩潰的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!