18

I am trying to learn how to use multiprocessingbut I can't get it to work. Here is the code right out of the documentation

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

it should output

>>> 'hello bob'

but instead i get

>>>

no errors or other messages, it just sits there, It is running in IDLE from a saved .py file on a Windows 7 machine with the 32-bit version of Python 2.7

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Devon M
  • 751
  • 2
  • 9
  • 24

6 Answers6

20

My guess is that you are using IDLE to try to run this script. Unfortunately, this example will not run correctly in IDLE. Note the comment at the beginning of the docs:

Note Functionality within this package requires that the main module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter.

The __main__ module is not importable by children in IDLE, even if you run the script as a file with IDLE (which is commonly done with F5).

Justin O Barber
  • 11,291
  • 2
  • 40
  • 45
  • I'm not running it from the interactive interpreter – Devon M Jan 18 '14 at 01:42
  • @Chuck Fulminata Where are you running it from? – Justin O Barber Jan 18 '14 at 01:43
  • a `.py` file saved on the hard drive – Devon M Jan 18 '14 at 01:44
  • @Chuck Fulminata And how are you running that file? – Justin O Barber Jan 18 '14 at 01:45
  • the exact procedure I go through to run it is right click, edit with IDLE, run module(f5) – Devon M Jan 18 '14 at 01:47
  • 2
    @Chuck Fulminata That is precisely the issue. That won't work, since the `__main__` module is not importable by children in IDLE, EVEN IF you are running the script as a .py file. – Justin O Barber Jan 18 '14 at 01:49
  • Oh, I didn't realize that, is that noted somewhere that I missed? – Devon M Jan 18 '14 at 01:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45516/discussion-between-chuck-fulminata-and-275365) – Devon M Jan 18 '14 at 01:54
  • @JustinBarber Your repeated claim that the user code in a .py file cannot be imported is wrong. The problem is trying to 'print' in a process without stdout. The solution is to make sure there is an stdout. See my answer. – Terry Jan Reedy Jul 28 '17 at 07:23
  • @πόδαςὠκύς Can you please help me with this, I do not know what to do, and it seams like similar problem: https://stackoverflow.com/questions/59892469/multiprocessing-for-webscrapping-wont-start-on-windows-and-mac – taga Jan 24 '20 at 21:51
10

The problem is not IDLE. The problem is trying to print to sys.stdout in a process that has no sys.stdout. That is why Spyder has the same problem. Any GUI program on Windows is likely to have the same problem.

On Windows, at least, GUI programs are usually run in a process without stdin, stdout, or stderr streams. Windows expects GUI programs to interact with users through widgets that paint pixels on the screen (the G in Graphical) and receive key and mouse events from Windows event system. That is what the IDLE GUI does, using the tkinter wrapper of the tcl tk GUI framework.

When IDLE runs user code in a subprocess, idlelib.run runs first, and it replaces None for the standard streams with objects that interact with IDLE itself through a socket. Then it exec()s user code. When the user code runs multiprocessing, multiprocessing starts further processes that have no std streams, but never get them.

The solution is to start IDLE in a console: python -m idlelib.idle (the .idle is not needed on 3.x). Processes started in a console get std streams connect to the console. So do further subprocesses. The real stdout (as opposed to the sys.stdout) of all the processes is the console. If one runs the third example in the doc,

from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

then the 'main line' block goes to the IDLE shell and the 'function f' block goes to the console.

This result shows that Justin Barber's claim that the user file run by IDLE cannot be imported into processes started by multiprocessing is not correct.

EDIT: Python saves the original stdout of a process in sys.__stdout__. Here is the result in IDLE's shell when IDLE is started normally on Windows, as a pure GUI process.

>>> sys.__stdout__
>>> 

Here is the result when IDLE is started from CommandPrompt.

>>> import sys
>>> sys.__stdout__
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
>>> sys.__stdout__.fileno()
1

The standard file numbers for stdin, stdout, and stderr are 0, 1, 2. Run a file with

from multiprocessing import Process
import sys

def f(name):
    print('hello', name)
    print(sys.__stdout__)
    print(sys.__stdout__.fileno())
if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

in IDLE started in the console and the output is the same.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
4

It works.

I've marked the changes needed to make your sample run using comments:

from multiprocessing import Process

def f(name):
print 'hello', name #indent

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()` # remove ` (grave accent)

result:

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

Output from my laptop after saving it as ex1.py:

reuts@reuts-K53SD:~/python_examples$ cat ex1.py 
#!/usr/bin/env python
from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()
reuts@reuts-K53SD:~/python_examples$ python ex1.py 
hello bob
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • 3
    Both of those were errors I made when I posted the question, neither are present in the actual code that won't do anything – Devon M Jan 18 '14 at 01:34
  • As you can see, this code does work. Either you're not using the same code I've posted, or the problem isn't the code. Where are you running the code? How are you running it? More information is needed. – Reut Sharabani Jan 18 '14 at 01:36
  • The problem may be specific to Windows, I don't know. Even on Windows, if you run IDLE from a command line, the output is visible. See my answer. – Terry Jan Reedy Jul 28 '17 at 07:29
3

I had the issue that multiprocessing did not work on Spyder, and always landed here. I solved it by using threading instead of multiprocessing. as described here: https://pymotw.com/2/threading/

import threading
def worker(num):
    """thread worker function"""
    print 'Worker: %s' % num
    return
threads = []
for i in range(5):
    t = threading.Thread(target=worker, args=(i,))
    threads.append(t)
    t.start()
Sami
  • 83
  • 1
  • 5
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Kalle Richter Jul 01 '17 at 14:44
  • Sami, if you can start Spyder from a console command line, the output should be visible, as it is for IDLE. (Are you running on Windows or ???.) – Terry Jan Reedy Jul 28 '17 at 07:31
  • Terry, I have not tried it and yes I'm running it on Windows. Karl: thank you I have added the examples. – Sami Jul 31 '17 at 12:57
2

Most likely your main process exits before sysout is flushed. Try this:

from multiprocessing import Process
import sys

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()
    # make sure all output has been processed before we exit
    sys.stdout.flush()

If this doesn't work, try adding time.sleep(1) as the last statement.

miraculixx
  • 10,034
  • 2
  • 41
  • 60
  • Though that might work, remember this code is verbatim from the documentation, and another poster has already noted that it works – Devon M Jan 18 '14 at 01:51
  • 1
    The code is fine, you just can't see the output. Multiprocessing behaviour depends on the particular machine you run it on, and yours might just be fast enough to swallow the output before you can see it. – miraculixx Jan 18 '14 at 01:53
  • Downvote all you like but please leave a comment so I can improve the answer. – miraculixx Dec 14 '16 at 04:06
  • Flushing is not the issue when there is no stdout to flush. Having a stdout stream solve the problem. See my answer. – Terry Jan Reedy Jul 28 '17 at 07:26
0

Try using this code (from standard manual). Works for me on windows. Another one did not work for me either :)

import multiprocessing as mp

    def foo(q):
        q.put('hello')
    
    if __name__ == '__main__':
        mp.set_start_method('spawn')
        q = mp.Queue()
        p = mp.Process(target=foo, args=(q,))
        p.start()
        print(q.get())
        p.join()
Vlad
  • 3
  • 4