Pathos multiprocessing 无法调用 class 中的任何包和函数
Pathos multiprocessing can't call any package and function in the class
我想在 class 中进行多处理。好像只有 pathos.multiprocessing 能帮到我。但是,当我实现它时,它无法加载我在主函数中使用的包。
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."
错误是没有定义全局名称'datetime'。但它在主要功能中有效!
我的系统是Win7.
我是 pathos
的作者。如果您在非 windows 系统上执行您的代码,它工作正常——即使是从解释器。 (它也适用于文件,也是如此)。
>>> 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)
...
>>> datetime.datetime.now()
datetime.datetime(2015, 10, 21, 19, 24, 16, 131225)
>>> t = tester()
>>> t.worker()
2015-10-21 19:24:25.927781
0
2015-10-21 19:24:27.933611
1
2015-10-21 19:24:29.938630
2
2015-10-21 19:24:31.942376
3
2015-10-21 19:24:33.946052
4
2015-10-21 19:24:35.949965
5
2015-10-21 19:24:37.953877
6
2015-10-21 19:24:39.957770
7
2015-10-21 19:24:41.961704
8
2015-10-21 19:24:43.965193
9
>>>
问题是 multiprocessing
在 windows 上根本不同,因为 windows 没有真正的 fork
... 因此不那么灵活就像在具有 fork
的系统上一样。 multiprocessing
有一个 forking pickler,它在幕后产生了一个 subprocess
… 而非 windows 系统可以跨进程使用共享内存。
dill
有一个 check
和一个 copy
方法在一些 object
上执行顺序 loads(dumps(object))
,其中 copy
使用共享内存,而 check
使用 subprocess
(与 multiprocessing
中的 windows 相同)。这是 mac 上的 check
方法,显然这不是问题所在。
>>> import dill
>>> dill.check(t.func)
<bound method tester.func of <__main__.tester instance at 0x1051c7998>>
您需要在 windows 上做的另一件事是在 __main__
的开头使用 freeze_support
(即 __main__
的第一行)。在非 windows 系统上没有必要,但在 windows 上非常必要。这是文档。
>>> import pathos
>>> print pathos.multiprocessing.freeze_support.__doc__
Check whether this is a fake forked process in a frozen executable.
If so then run code specified by commandline and exit.
>>>
我想在 class 中进行多处理。好像只有 pathos.multiprocessing 能帮到我。但是,当我实现它时,它无法加载我在主函数中使用的包。
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."
错误是没有定义全局名称'datetime'。但它在主要功能中有效! 我的系统是Win7.
我是 pathos
的作者。如果您在非 windows 系统上执行您的代码,它工作正常——即使是从解释器。 (它也适用于文件,也是如此)。
>>> 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)
...
>>> datetime.datetime.now()
datetime.datetime(2015, 10, 21, 19, 24, 16, 131225)
>>> t = tester()
>>> t.worker()
2015-10-21 19:24:25.927781
0
2015-10-21 19:24:27.933611
1
2015-10-21 19:24:29.938630
2
2015-10-21 19:24:31.942376
3
2015-10-21 19:24:33.946052
4
2015-10-21 19:24:35.949965
5
2015-10-21 19:24:37.953877
6
2015-10-21 19:24:39.957770
7
2015-10-21 19:24:41.961704
8
2015-10-21 19:24:43.965193
9
>>>
问题是 multiprocessing
在 windows 上根本不同,因为 windows 没有真正的 fork
... 因此不那么灵活就像在具有 fork
的系统上一样。 multiprocessing
有一个 forking pickler,它在幕后产生了一个 subprocess
… 而非 windows 系统可以跨进程使用共享内存。
dill
有一个 check
和一个 copy
方法在一些 object
上执行顺序 loads(dumps(object))
,其中 copy
使用共享内存,而 check
使用 subprocess
(与 multiprocessing
中的 windows 相同)。这是 mac 上的 check
方法,显然这不是问题所在。
>>> import dill
>>> dill.check(t.func)
<bound method tester.func of <__main__.tester instance at 0x1051c7998>>
您需要在 windows 上做的另一件事是在 __main__
的开头使用 freeze_support
(即 __main__
的第一行)。在非 windows 系统上没有必要,但在 windows 上非常必要。这是文档。
>>> import pathos
>>> print pathos.multiprocessing.freeze_support.__doc__
Check whether this is a fake forked process in a frozen executable.
If so then run code specified by commandline and exit.
>>>