Help with Python Multiprocessing

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Fri Nov 14 14:41:10 EST 2014


On Thursday, November 13, 2014 3:22:49 PM UTC-8, Anurag wrote:
> On Thursday, November 13, 2014 2:18:50 PM UTC-5, sohca... at gmail.com wrote:
> > On Thursday, November 13, 2014 10:07:56 AM UTC-8, Anurag wrote:
> > > I am having trouble understanding the Multiprocessing module.
> > > I need to run three different files 'Worker1' , 'Worker2', 'Worker3' all at once. Currently I am doing this :
> > > 
> > > from multiprocessing import Process
> > > 
> > > import Worker1.py
> > > import Worker2.py
> > > import Worker3.py
> > > 
> > > 
> > > 
> > > p1 = Process(target=Worker1.py) 
> > > p1.start()
> > > p2 = Process(target=Worker2.py)
> > > p2.start()
> > > p3 = Process(target=Worker3.py)
> > > p3.start()
> > > 
> > > But this will only start the 'Worker1'. How do I execute all the three files at once?
> > > 
> > > Thanks
> > 
> > Do your WorkerX.py files have a main() function or anything like that?  If not, they should.  Then, you'd set the targets to WorkerX.main.
> 
> My Worker files have three different functions

What I mean is that your code should probably look more like this:

# Contents of main.py
from multiprocessing import Process
import Worker1
import Worker2
import Worker3

p1 = Process(target=Worker1.main)
p1.start()
p2 = Process(target=Worker2.main)
p2.start()
p3 = Process(target=Worker3.main)
p3.start

# Contents of Worker1.py
def main():
    # Do worker1 stuff...


# Contents of Worker2.py
def main():
    # Do worker2 stuff...


# Contents of Worker3.py
def main():
    # Do worker3 stuff...

Alternatively, you could have a single worker.py, import that, but have 3 main() functions, main1(), main2(), main3(), and set the targets for each process to those functions.

Maybe its because I'm less experienced as most people on this group, but setting a Process's target to a module and not a specific function in that module seems slightly strange and ambiguous to me.



More information about the Python-list mailing list