Reputation: 105
Multiprocessing Process class example in python documentary seem to return None. I copy pasted the example and run it, but it returns only ">>>" instead of printing 'hello', name. Tested in 2.7.11 and 3.5.1. I must be missing something obvious here, but can't figure out what.
from multiprocessing import Process
def f(name):
print('hello', name)
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
--Edit-- It works in Linux Ubuntu 14.04. Not in Windows 7. --Edit-- Seems that the main issue is that whether it is run from IDLE or from file. Works when run from file.
Upvotes: 0
Views: 619
Reputation: 21831
Since you mention the >>>
prompt, it sounds like you are running the example in the interactive interpreter?
In that case, the if __name__ == "__main__"
line will never evaluate to True
and thus never run the code you want. You can read about this in the documentation.
Try either saving to a script and run that script, or just remove the if
-clause:
from multiprocessing import Process
def f(name):
print('hello', name)
p = Process(target=f, args=('bob',))
p.start()
p.join()
Upvotes: 1
Reputation: 14369
At first there is a large difference between returning and printing. Your code has no return
so it is only printing.
An other process has a different stdout
associated to which printed text will go. You will not see it until you explicitly connect it to the stdout
of the main process or process it in an other way. The subprocess.Popen()
allows to do that, multiprocessing.Process()
does not as far as I am aware.
Upvotes: 0