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

Community
  • 1
  • 1
Ehsan
  • 73
  • 11
  • 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. – zwer Jun 06 '18 at 09: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. – Ketan Mukadam Jun 06 '18 at 09:17
  • @zwer I just a typo. the problem is not solved – Ehsan Jun 06 '18 at 09: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` – Ehsan Jun 06 '18 at 09: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 – Martin Thoma Jun 06 '18 at 09:36
  • Why do you expect "Done" first? – Martin Thoma Jun 06 '18 at 09:38
  • @MartinThoma No I corrected two typos. I just copied and pasted an example from the mentioned website. Is my python version has a problem? – Ehsan Jun 06 '18 at 09:56
  • I tried it with "Python 2.7.12" and [this version](https://stackoverflow.com/revisions/50716403/4) worked fine. – Martin Thoma Jun 06 '18 at 11:05

2 Answers2

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

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
  • In this case I see `Done` Again. I am getting mad. I could not manage to find the problem :(( – Ehsan Jun 06 '18 at 09:26
  • Do you think I have to change my Python version? – Ehsan Jun 06 '18 at 09:57
  • How do you run the script? – Jacques Gaudin Jun 06 '18 at 10:00
  • 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. – Ehsan Jun 06 '18 at 10:09
  • Try to open a command line, go to the directory where your file is saved and type `python myfile.py`. – Jacques Gaudin Jun 06 '18 at 10:19
  • Interestingly enough, it doesn't run in IDLE on my machine either, I get `module 'idlelib.pyshell' has no attribute 'use_subprocess'` – Jacques Gaudin Jun 06 '18 at 10:23
  • So, Where did you run this code when IDLE does not work? – Ehsan Jun 06 '18 at 10:25
  • At the moment I use spyder3 which is pretty good for small snippets, for larger projects I use atom. You can run any script from the command line with `python myscript.py`. If you want spyder under windows, you should get anaconda. – Jacques Gaudin Jun 06 '18 at 10:28
  • I think I've found a [related question here](https://stackoverflow.com/questions/35293178/can-multiprocessing-process-class-be-run-from-idle) – Jacques Gaudin Jun 06 '18 at 10:31
  • I have Anaconda and this code does not work on `Jupiter`. And I do not understand the command line you told me. I mean I do not understand what should I do. – Ehsan Jun 06 '18 at 10:32
  • Yes this is the exact problem of mine this question mentioned. But Do you know what is consol 2.x.? in the answer of question is mentioned. – Ehsan Jun 06 '18 at 10:41
  • If you are on windows, you can get a terminal with `win+r` and type `cmd`. From there move to the directory where your file is located with `cd c:\Users\Ehsan\mydir` then type `python myfile.py` – Jacques Gaudin Jun 06 '18 at 10:41
  • What they mean is `python 2.x` not `console 2.x`. – Jacques Gaudin Jun 06 '18 at 10:44
  • so it needs again python 2x. A weakness in Python programming – Ehsan Jun 06 '18 at 10:47
  • No `C:\Users\MyName>python -m idlelib` is for Python 3.x and `C:\Users\MyName>python -m idlelib.idle` is for Python 2.x. – Jacques Gaudin Jun 06 '18 at 10:49
  • great, I works. But I have a question. Is there any other way to do? and consider instead of the first print, we write the outputs in a file. after doing this in `idlelib` it will do our order? – Ehsan Jun 06 '18 at 11:02
  • I don't understand. If you have another question post it separately. Long discussions like this one are frowned upon here. – Jacques Gaudin Jun 06 '18 at 11:07
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.

Ethan
  • 180
  • 2
  • 15