I want to do multiprocessing in the class. It seems like only pathos.multiprocessing is able to help me. However, when I implement it, it can't load the packages I use in the main function.
from pathos.multiprocessing import ProcessingPool;
import time
import sys;
import datetime
class tester:
def __init__(self):
self.pool=ProcessingPool(2);
def func(self,msg):
print (str(datetime.datetime.now()));
for i in xrange(1):
print msg
sys.stdout.flush();
time.sleep(2)
#----------------------------------------------------------------------
def worker(self):
""""""
pool=self.pool
for i in xrange(10):
msg = "hello %d" %(i)
pool.map(self.func,[i])
pool.close()
pool.join()
time.sleep(40)
if __name__ == "__main__":
print datetime.datetime.now();
t=tester()
t.worker()
time.sleep(60);
print "Sub-process(es) done."
the wrong is that global name 'datetime' is not defined. But it works in the main function! My sys is Win7.
I'm the author of
pathos
. If you execute your code on non-windows systems, it works fine -- even from the interpreter. (It also works from a file, as is too).The issue is that
multiprocessing
fundamentally is different on windows, in that windows doesn't have a truefork
… and thus isn't as flexible as on systems with afork
.multiprocessing
has a forking pickler, that under the covers spawns asubprocess
… while non-windows systems can utilize shared memory across the processes.dill
has acheck
and acopy
method that does a sequentialloads(dumps(object))
on someobject
, wherecopy
uses shared memory, whilecheck
usessubprocess
(as is done on windows inmultiprocessing
). Here's thecheck
method on a mac, so apparently that's not the issue.The other thing you need to do on windows, is to use
freeze_support
at the beginning of__main__
(i.e. the first line of__main__
). It's unnecessary on non-windows systems, but pretty much necessary on windows. Here's the doc.