2013-05-07 57 views
0

Windows支持multithreading.pipes()嗎?如果是,那麼這個代碼有什麼問題?我需要使用還原嗎?代碼掛在p2.recv()上,從命令行運行時,我得到一個RuntimeError。使用python在窗口上多處理管道

import multiprocessing 
def ProcessCreator(pipe): 
    pipe.send("hello from other process") 

p1, p2 = multiprocessing.Pipe() 
proc = multiprocessing.Process(target = ProcessCreator, args = (p2,)) 
proc.start() 
print p1.recv() 

if __name__ == "__main__": 
    multiprocessing.freeze_support() 

回答

1

你需要把管道代碼爲如果__name__ == '__main__'一部分。(Why?)和更改p2.recvp1.recv

import multiprocessing 
def ProcessCreator(pipe): 
    pipe.send("hello from other process") 

if __name__ == "__main__": 
    multiprocessing.freeze_support() 
    p1, p2 = multiprocessing.Pipe() 
    proc = multiprocessing.Process(target = ProcessCreator, args = (p2,)) 
    proc.start() 
    print p1.recv()