2016-11-10 9 views
0

Я пытаюсь выполнить многопроцессорную функцию, которая выполняет несколько действий для большого файла, но я получаю сообщение об ошибке pickling, хотя Im использует partial.многопроцессор python с несколькими аргументами

функция выглядит примерно так:

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 где также QUEUES ...

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

Смежные вопросы