1

Here is my multiprocessing code.

from multiprocessing import Process

def print_func(continent = 'Asia'):
    print('The name of continent is: ',continent)

if __name__ == "__main__":
    names = ['America','Russia','Africa']
    procs = []
    proc = Process(target = print_func)
    procs.append(proc)
    proc.start()

    for name in names:
        proc = Process(target = print_func,args =(name,))
        procs.append(proc)
        proc.start()

    for proc in procs:
        proc.join()

But it shows no output! What am I doing wrong?

I want output like this:

The name of continent is: Asia
The name of continent is: America
The name of continent is: Russia
The name of continent is: Africa
karel
  • 5,489
  • 46
  • 45
  • 50
Chidananda Nayak
  • 175
  • 1
  • 1
  • 11

3 Answers3

2

As described in this answer and this bug report, this is a design limitation of IDLE. Since IDLE connects only to the started process and not its children, it does not catch the output.

Run the code in another Python IDE (IDLE is chiefly aimed at beginners), or just run the program without an IDE, i.e. (assuming your program file is called multiprint.py) with

python multiprint.py
phihag
  • 278,196
  • 72
  • 453
  • 469
  • Im more confused now, i tried to run it in Pycharm and Wing, both showing same error, i mean now they show various import errors no_module _named_sandbox, but the same code is working fine with yours! i hate my life! – Chidananda Nayak May 12 '18 at 11:45
  • 1
    There's an web app for solving that problem named [repl.it](https://repl.it/). Select your language (Python 3), copy/paste your code into the repl.it IDE, run it, and it should give the correct output (it gave the correct output for me at least, and I also got the correct output in PyCharm). If your code runs in repl.it, the next thing to check is if there is anything wrong with your project in PyCharm that is causing the import errors. – karel May 12 '18 at 14:42
  • @karel oh god! thanks, it worked in repl.it ! thanks a lot for your advice.. so now i have to fix my pycharm.. – Chidananda Nayak May 12 '18 at 17:56
  • 1
    I love using PyCharm, and I hope you can fix it. – karel May 12 '18 at 17:58
0

The explanation is in the IDLE doc section Running User code.

By default, IDLE runs user code in a separate OS process rather than in the user interface process that runs the shell and editor. In the execution process, it replaces sys.stdin, sys.stdout, and sys.stderr with objects that get input from and send output to the Shell window. The original values stored in sys.stdin, sys.stdout, and sys.stderr are not touched, but may be None.

...

IDLE’s standard stream replacements are not inherited by subprocesses created in the execution process, whether directly by user code or by modules such as multiprocessing. If such subprocess use input from sys.stdin or print or write to sys.stdout or sys.stderr, IDLE should be started in a command line window. (On Windows, use python or py rather than pythonw or pyw.) The secondary subprocess will then be attached to that window for input and output.

I started IDLE in Windows Command Prompt with py -m idlelib. Then I pasted your code into Editor and ran it. The expected output appeared in Command Prompt, after the command line.

Other GUI IDEs will tend to have the same issue.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
-2

My Stackoverflow senses tell mey you are pasting this in the python shell. You need to put it into a python file, e.g. continents.py , and then execute the interpreter like so

python continents.py

Or, you can remove the if __name__ == "__main__": and it should work also directly when pasted into the python shell.

See it in action here.

vidstige
  • 12,492
  • 9
  • 66
  • 110