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
  • 4,637
  • 41
  • 42
  • 47
Chidananda Nayak
  • 105
  • 1
  • 1
  • 9

2 Answers2

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
  • 263,143
  • 67
  • 432
  • 458
  • 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
-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
  • 11,888
  • 8
  • 65
  • 104