2016-11-10 7 views
0

대용량 파일에 대해 여러 작업을 수행하는 함수를 다중 처리하려고하지만 partial을 사용하는 알려진 오류 pickling 오류가 발생합니다.다중 인수를 사용하는 파이썬 다중 처리

기능은 다음과 같은 :

def process(r,intermediate_file,record_dict,record_id): 

    res=0 

    record_str = str(record_dict[record_id]).upper() 
    start = record_str[0:100] 
    end= record_str[len(record_seq)-100:len(record_seq)] 

    print sample, record_id 
    if r=="1": 

     if something: 
      res = something... 
      intermediate_file.write("...") 

     if something: 
      res = something 
      intermediate_file.write("...") 



    if r == "2": 
     if something: 
      res = something... 
      intermediate_file.write("...") 

     if something: 
      res = something 
      intermediate_file.write("...") 

    return res 

는 다른 함수에 따르고 호출 방법 메신저 :

def call_func(): 
    intermediate_file = open("inter.txt","w") 
    record_dict = get_record_dict()     ### get infos about each record as a dict based on the record_id 
    results_dict = {} 
    pool = Pool(10) 
    for a in ["a","b","c",...]: 

     if not results_dict.has_key(a): 
      results_dict[a] = {} 

     for b in ["1","2","3",...]: 

      if not results_dict[a].has_key(b): 
       results_dict[a][b] = {} 


      results_dict[a][b]['res'] = [] 

      infile = open(a+b+".txt","r") 
      ...parse the file and return values in a list called "record_ids"... 

      ### now call the function based on for each record_id in record_ids 
      if b=="1": 
       func = partial(process,"1",intermediate_file,record_dict) 
       res=pool.map(func, record_ids) 
       ## append the results for each pair (a,b) for EACH RECORD in the results_dict 
       results_dict[a][b]['res'].append(res) 

      if b=="2": 
       func = partial(process,"2",intermediate_file,record_dict) 
       res = pool.map(func, record_ids) 
       ## append the results for each pair (a,b) for EACH RECORD in the results_dict 
       results_dict[a][b]['res'].append(res) 

    ... do something with results_dict... 

아이디어는 record_ids 내부의 각 레코드에 대해, 내가 원하는 것입니다 각 쌍 (a, b)에 대한 결과를 저장합니다.

나는 날이 오류를주고있다 모르겠어요 :

File "/code/Python/Python-2.7.9/Lib/multiprocessing/pool.py", line 251, in map 
    return self.map_async(func, iterable, chunksize).get() 
    File "/code/Python/Python-2.7.9/Lib/multiprocessing/pool.py", line 558, in get 
    raise self._value 
cPickle.PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function faile 

D가 절인 수 없도록

답변

0

func이 코드의 최상위 수준에 정의되어 있지 않습니다. pathos.multiprocesssing은 표준 모듈이 아니지만 작동 할 수 있습니다.

또는 Pool.map과 다른 번호를 사용할 수도 있습니다. 결국 https://docs.python.org/2/library/queue.html

는 ... 그것은 threading위한거야뿐만 아니라이 큐에 포함되어있는 multiprocessing 매우 유사합니다, 당신이 사용할 수있는 예제가

https://docs.python.org/2/library/multiprocessing.html#pipes-and-queues

관련 문제