Multiprocessing pipes on windows with python

2020-05-03 11:27发布

问题:

Does Windows support multithreading.pipes()? If yes, then what is wrong with this code? Do I need to be using reduction? The code hangs on p2.recv() and I get a RuntimeError when run from the command line.

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:

You need put pipe code into if __name__ == '__main__' part.(Why?) And change p2.recv to 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()