0

I just want to run an example from this site which is the following code:

from multiprocessing import Process


def square(x):

    for x in numbers:
        print('%s squared  is  %s' % (x, x**2))

if __name__ == '__main__':
        numbers = [43, 50, 5, 98, 34, 35]

    p = Process(target=square, args=('x',))
    p.start()
    p.join()
    print ("Done")
 

it says that if you run the code, you should see the following results:

#result
Done
43 squared  is  1849
50 squared  is  2500
5 squared  is  25
98 squared  is  9604
34 squared  is  1156
35 squared  is  1225

But as I run this code, I just see Done in results and nothing more. In that Tutorial used Python 2.7 but I have 3.6 and I added () in prints.

Thank you for your help

8
  • You forgot to call p.join() (notice the brackets after join()) to wait for the process to finish. Also, in case you didn't make an error when indenting your code on SO, keep in mind that indentation is very important in Python. Commented Jun 6, 2018 at 9:10
  • p.join() just ensures main process waits for children to finish. So you would see Done at the end instead of first print as shown in output by OP. Are you indentations correct? When I ran the same code on my python 3.5.2, it works nicely. Commented Jun 6, 2018 at 9:17
  • @zwer I just a typo. the problem is not solved Commented Jun 6, 2018 at 9:20
  • @KetanMukadam () is added, it was typo. But i do not know why it does not work in my python 3.6.3. I just see Done Commented Jun 6, 2018 at 9:20
  • @Ehsan Editing simple typos is fine, but you changed quite a bit since you asked your question. Now, the code you have in your question is working which is confusing when one reads the question Commented Jun 6, 2018 at 9:36

2 Answers 2

2

There are a few problems with your snippet.

  1. Identation: if __name__ == '__main__': is part of the square function
  2. The functionsquare should take the argument numbers not x
  3. The arguments passed to the process should be args=(numbers,)

Once you fix all this, you get:

from multiprocessing import Process


def square(numbers):

    for x in numbers:
        print('%s squared  is  %s' % (x, x**2))

if __name__ == '__main__':
    numbers = [43, 50, 5, 98, 34, 35]

    p = Process(target=square, args=(numbers,))
    p.start()
    p.join()
    print ("Done")

which runs correctly.

If you use IDLE on Windows, to see the output, you need to start IDLE from the command line:

C:\Users\MyName>python -m idlelib

as explained in this question

Sign up to request clarification or add additional context in comments.

17 Comments

In this case I see Done Again. I am getting mad. I could not manage to find the problem :((
Do you think I have to change my Python version?
How do you run the script?
Copying and pasting the code in the white page of Python and the running it. The results will be appeared in Python shell >>> I use IDLE.
Try to open a command line, go to the directory where your file is saved and type python myfile.py.
|
0

If IDLE quits with no message, and it was not started from a console, try starting from a console (python -m idlelib) and see if a message appears. For example if your python file is in the Desktop you may write

C:\Users\MyName> Desktop\python-file-name -m idlelib

prss win + x and choose command prompt.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.