問(wèn)題描述
我在使用 Python 多處理模塊時(shí)遇到問(wèn)題.我正在使用 Process
類(lèi)來(lái)生成一個(gè)新進(jìn)程,以便利用我的第二個(gè)核心.第二個(gè)過(guò)程將一堆數(shù)據(jù)加載到 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.
我想看看那個(gè)進(jìn)程用 print
命令打印了什么,但是,我沒(méi)有看到它打印的任何東西.我只看到父進(jìn)程打印的內(nèi)容.現(xiàn)在這對(duì)我來(lái)說(shuō)很有意義,因?yàn)樗麄兩钤趦蓚€(gè)不同的過(guò)程中.第二個(gè)進(jìn)程不會(huì)產(chǎn)生自己的 shell/標(biāo)準(zhǔn)輸出窗口,也不會(huì)將其輸出發(fā)送到父進(jìn)程.然而,當(dāng)這個(gè)進(jìn)程崩潰時(shí),它會(huì)打印我的腳本告訴它打印的所有內(nèi)容,以及堆棧跟蹤和錯(cuò)誤.
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.
我想知道是否有一種簡(jiǎn)單的方法可以將子進(jìn)程的打印輸出發(fā)送到第一個(gè)進(jìn)程,或者讓它產(chǎn)生一個(gè) shell/標(biāo)準(zhǔn)輸出,以便我可以調(diào)試它.我知道我可以創(chuàng)建一個(gè) multiprocessing.Queue
專(zhuān)用于將打印傳輸?shù)礁讣?jí),以便它可以將這些打印到標(biāo)準(zhǔn)輸出,但如果存在更簡(jiǎn)單的解決方案,我不想這樣做.
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.
推薦答案
你試過(guò)刷新標(biāo)準(zhǔn)輸出嗎?
Have you tried flushing stdout?
import sys
print "foo"
sys.stdout.flush()
這篇關(guān)于子 multiprocessing.Process 沒(méi)有打印輸出,除非程序崩潰的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!