我正在尝试使用多处理在python中编写一个简单的测试程序。我正在使用 Pool.map() .对于它,我传递了子进程应该调用的方法。它工作正常,当返回类型为 python 内置类型(例如字符串、日期时间等)时,我得到了预期的结果。但是,当我使用自定义类作为返回类型时,我的进程就会挂起。不确定我是否在做正确的事情,任何建议将不胜感激。这是我下面的代码:
from multiprocessing import Pool
from multiprocessing.pool import ApplyResult
import time
import os
import logging
import multiprocessing
import sys
class C( object ):
def __init__(self, a, b=1):
self.a = a
self.b = b
def f(self, name):
time.sleep(2)
#Doing some processing using custom classes
#and generating a result of custom type
x = someMethodInCustomClass()
# x now has list of list of custom objects eg x =[[c1,c2],[c3,c4]]
return x
#Above doesn't work. Returning string/datetime instead of x works fine.
def _run(self):
print 'Reached inside run..'
print 'main process id ..', os.getpid()
pool = Pool(processes=6)
names = ['frank', 'justin', 'osi', 'thomas', 'jim', 'rock', 'stonecold', 'machoman']
result = pool.map(unwrap_self_f, zip([self]*len(names), names), 1)
print type(result)
print result
def hello(self):
print 'Running hello....'
self._run()
def test():
logger.info( 'Starting....test')
c = C(1, 2)
print 'Running test....'
c.hello()
def unwrap_self_f(arg, **kwarg):
print 'inside unwrap_self_f'
return C.f(*arg, **kwarg)
if __name__ == '__main__':
print 'Loading multiprocessing...'
logger = multiprocessing.log_to_stderr()
logger.setLevel(logging.DEBUG)
test()
我正在使用Python 2.6.6。操作系统是窗口-32位/64位。
pool.map 返回一个没有wait
方法的iterable
。
result = pool.map(unwrap_self_f, zip([self]*len(names), names), 1)
result.wait() # <--- no such thing
没有必要等待,因为"它会阻止直到结果准备好"。