I am trying to implement multiprocessing into python on windows environment. I know multiprocessing have to start with if __name__=='main'. But my problem is that I have a long pipeline of functions and first part of code is running as single process but output of first step which is a list needs to be run parallely as it takes lot of time. So my question is how to run output of a single process function into multiple processes.
One solution is keeping multiprocess code in some other script under main and call it using os.system from main script.
MainScript.py
import os
....
......
....
names = [('frank', 'surname'), ('justin', 'surname'), ('osi', 'surname'), ('thomas', 'surname')]
cmd = 'python fortest.py %s' % names
os.system(cmd)
MultiProcessCode.py
from multiprocessing import Pool
import time, sys
class B:
def __init__(self):
self.__c = C()
def run_C(self):
rslt = self.__c.run()
print rslt
def unwrap_fms(arg):
return C.f(*arg)
class C:
def __init__(self, print_init = 'Initialized'):
print print_init
def f(self, name):
return name[0]
#print "hello %s %s" %(name[0], name[1])
def run(self):
pool = Pool(processes=2)
names = [('frank', 'surname'), ('justin', 'surname'), ('osi', 'surname'), ('thomas', 'surname')]
#print zip([self]*len(names), names)
rslt = pool.map(unwrap_fms, zip([self]*len(names), names))
return rslt
if __name__ == '__main__':
b =B()
b.run_C()
But my input list is too big, so is there a better way to do this and takes a couple of objects as well. Or if I can put things in one file itself