マルチプロセッシングを使用してPythonで簡単なテストプログラムを作成しようとしています。私は Pool.map() を使用しています。それに、子プロセスによって呼び出されるメソッドを渡しています。戻り値の型が python 組み込み型 (string、datetime など) の場合、正しく動作し、期待どおりの結果が得られます。ただし、カスタム クラスを戻り値の型として使用すると、プロセスがハングします。私が正しいことをしているかどうかわからないので、どんな提案でも大歓迎です。ここに私のコードがあります:
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 を使用しています。OSはwindows-32bit/64bitです。