Recently, a reader wrote in about a problem they were having with Multiprocessing in Windows and Python. Since I’ve actually had this come up several times before, I decided I would go ahead and write a quick explanation about it for others that might have the same problem.
If you’ve ever done any kind of parallel programming, you will know that it is especially easy to do with Python. All you have to do is define a function you want to handle some work and pass that function to the multiprocessing.Process method. Here is a quick example:
from multiprocessing import Process def child(i): print "Child:", i if __name__ == '__main__': for i in range(0, 5): p = Process(target = child, args = (i,)) p.start() p.join()
This code works exactly as it should. When ran, 5 new instances of “pythonw.exe” get spawned where each new instance prints “Child: X” where “X” is the instance number passed in as a parameter. However, if you run this code from Idle on Windows, nothing gets printed to the screen. It looks like the code is not working, but in reality it is. The reason nothing gets printed in Idle is because each instance gets spawned inside a new process (pythonw.exe). Since your console is attached to your currently running instance of Idle, the “Child: X” message gets printed on each of the hidden instances which you cannot see.
To test this code, you will need to run the application from a command prompt instead from inside Idle. So, go ahead and click on Start > Run and type “cmd” and click the “OK” button. This will launch a new command prompt. Use the “cd” command to change directory into the directory that contains your Python script. Once there, type the name of your Python script and press enter. You should now see something like this:
That’s it. If you see something like above, then you have now successfully created a simple multiprocessing application using Python.