0

我正在尝试使用多处理在 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。操作系统是 windows-32 位/64 位。

4

1 回答 1

0

pool.map返回一个iterable没有wait方法的。

    result = pool.map(unwrap_self_f, zip([self]*len(names), names), 1)
    result.wait() # <---  no such thing

无需等待,因为“它会阻塞,直到结果准备好。”。

于 2014-12-03T09:55:23.480 回答