나는 다중 처리를 사용하여 파이썬에서 간단한 테스트 프로그램을 작성하려고합니다. Pool.map()을 사용하고 있습니다. 그것에, 나는 자식 프로세스에 의해 호출되어야하는 메소드를 전달하고있다. 올바르게 작동하고 반환 형식이 파이썬 기본 제공 형식 (예 : 문자열, 날짜/시간 등) 일 때 예상되는 결과가 나타납니다. 그러나 사용자 지정 클래스를 반환 형식으로 사용하면 프로세스가 중단됩니다. 내가 옳은 일을하고 있는지 확실하지 않다면, 어떤 제안이라도 높게 평가 될 것입니다. 여기 내 코드는 다음과 같습니다.파이썬의 풀 맵 메서드를 사용할 때 사용자 지정 개체를 반환 할 수 없습니다.
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입니다.
클래스를 반환하지 않으면 클래스 내의 인스턴스 메소드에 대한 호출을 반환합니다. 'return Cf (* arg, ** kwarg)' –