Sai Sai - 3 months ago 23
Python Question

Is there any interpreter that works well for running multiprocessing in python 2.7 version on windows 7?

I was trying to run a piece of code.This code is all about multiprocessing.It works fine on command prompt and it also generates some output.But when I try to run this code on pyscripter it just says that script runs ok and it doesn't generate any output nor even it displays any error message.It doesn't even crashes.It would be really helpful if anyone could help me out to find out a right interpreter where this multiprocessing works fine.
Here is the piece of code:

from multiprocessing import Process
def wait():
print "wait"
clean()
def clean():
print "clean"
def main():
p=Process(target=wait)
p.start()
p.join()
if _name_=='_main_':
main()

Answer

The normal interpreter works just fine with multiprocessing on Windows 7 for me. (Your IDE might not like multiprocessing.)

You just have to do

if __name__=='__main__':
    main()

with 2 underscores (__) each instead of 1 (_).

Also - if you don't have an actual reason not to use it, multiprocessing.Pool is much easier to use than multiprocessing.Process in most cases. Have alook at https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool

An implementation with a Pool would be

import multiprocessing
def wait():
    print "wait"
    clean()
def clean():
    print "clean"
def main():
    p=multiprocessing.Pool()
    p.apply_async(wait)
    p.close()
    p.join()
if __name__=='__main__':
    main()

but which method of Pool to use strongly depends on what you actually want to do.

Comments