0

Windows 是否支持 multithreading.pipes()?如果是,那么这段代码有什么问题?我需要使用减少吗?代码挂在 p2.recv() 上,从命令行运行时出现 RuntimeError 。

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()
4

1 回答 1

2

您需要将管道代码放入if __name__ == '__main__'部分。(为什么?)并将p2.recv更改为p1.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()
于 2013-05-07T02:30:18.960 回答