i trying first formal python program using threading , multiprocessing on windows machine. unable launch processes though, python giving following message. thing is, not launching threads in main module. threads handled in separate module inside class.
edit: way code runs fine on ubuntu. not quite on windows
runtimeerror: attempt start new process before current process has finished bootstrapping phase. means on windows , have forgotten use proper idiom in main module: if __name__ == '__main__': freeze_support() ... "freeze_support()" line can omitted if program not going frozen produce windows executable.
my original code pretty long, able reproduce error in abridged version of code. split in 2 files, first main module , little other import module handles processes/threads , calls method. second module meat of code is.
testmain.py:
import paralleltestmodule extractor = paralleltestmodule.parallelextractor() extractor.runinparallel(numprocesses=2, numthreads=4)
paralleltestmodule.py:
import multiprocessing multiprocessing import process import threading class threadrunner(threading.thread): """ class represents single instance of running thread""" def __init__(self, name): threading.thread.__init__(self) self.name = name def run(self): print self.name,'\n' class processrunner: """ class represents single instance of running process """ def runp(self, pid, numthreads): mythreads = [] tid in range(numthreads): name = "proc-"+str(pid)+"-thread-"+str(tid) th = threadrunner(name) mythreads.append(th) in mythreads: i.start() in mythreads: i.join() class parallelextractor: def runinparallel(self, numprocesses, numthreads): myprocs = [] prunner = processrunner() pid in range(numprocesses): pr = process(target=prunner.runp, args=(pid, numthreads)) myprocs.append(pr) # if __name__ == 'paralleltestmodule': #this didnt work # if __name__ == '__main__': #this doesnt work # multiprocessing.freeze_support() #added after seeing error no avail in myprocs: i.start() in myprocs: i.join()
on windows subprocesses import (i.e. execute) main module @ start. need protect main code avoid creating subprocesses recursively:
import paralleltestmodule if __name__ == '__main__': extractor = paralleltestmodule.parallelextractor() extractor.runinparallel(numprocesses=2, numthreads=4)
Comments
Post a Comment