2011-09-16 188 views
61

我試圖重寫一些csv閱讀代碼,以便能夠在Python 3.2.2的多個核心上運行它。我嘗試使用多處理的Pool對象,我從工作示例中調整了它(並且已經爲我的項目的另一部分工作)。我遇到了一個我很難解讀和排除故障的錯誤消息。解決此問題的好方法是什麼?謝謝!如何解決Python中multiproccesing中的「AttributeError:__exit__」問題?

錯誤:

Traceback (most recent call last): 
    File "parser5_nodots_parallel.py", line 256, in <module> 
    MG,ppl = csv2graph(r) 
    File "parser5_nodots_parallel.py", line 245, in csv2graph 
    node_chunks) 
    File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/multiprocessing/pool.py", line 251, in map 
    return self.map_async(func, iterable, chunksize).get() 
    File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/multiprocessing/pool.py", line 552, in get 
    raise self._value 
AttributeError: __exit__ 

相關的代碼:

import csv 
import time 
import datetime 
import re 
from operator import itemgetter 
from multiprocessing import Pool 
import itertools 

def chunks(l,n): 
    """Divide a list of nodes `l` in `n` chunks""" 
    l_c = iter(l) 
    while 1: 
     x = tuple(itertools.islice(l_c,n)) 
     if not x: 
      return 
     yield x 

def csv2nodes(r): 
    strptime = time.strptime 
    mktime = time.mktime 
    l = [] 
    ppl = set() 
    pattern = re.compile(r"""[A-Za-z0-9"/]+?(?=[,\n])""") 
    for row in r: 
     with pattern.findall(row) as f: 
      cell = int(f[3]) 
      id = int(f[2]) 
      st = mktime(strptime(f[0],'%d/%m/%Y')) 
      ed = mktime(strptime(f[1],'%d/%m/%Y')) 
     # collect list 
     l.append([(id,cell,{1:st,2: ed})]) 
     # collect separate sets 
     ppl.add(id) 
    return (l,ppl) 

def csv2graph(source): 
    MG=nx.MultiGraph() 
    # Remember that I use integers for edge attributes, to save space! Dic above. 
    # start: 1 
    # end: 2 
    p = Pool() 
    node_divisor = len(p._pool) 
    node_chunks = list(chunks(source,int(len(source)/int(node_divisor)))) 
    num_chunks = len(node_chunks) 
    pedgelists = p.map(csv2nodes, 
         node_chunks) 
    ll = [] 
    ppl = set() 
    for l in pedgelists: 
     ll.append(l[0]) 
     ppl.update(l[1]) 
    MG.add_edges_from(ll) 
    return (MG,ppl) 

with open('/Users/laszlosandor/Dropbox/peers_prisons/python/codetenus_test.txt','r') as source: 
    r = source.readlines() 
    MG,ppl = csv2graph(r) 
+1

就我而言,我不小心被傳遞'None'由於作用域的問題。 – ThorSummoner

+0

當我將類聲明爲'Class SomeClass(object):'時,我曾經這樣做過,即使我的DID在我的類中顯式地有__exit__。一旦我從'object'中移除繼承,它就起作用了。我不知道爲什麼,所以YMMV – mpag

回答

111

的問題是在這條線:

with pattern.findall(row) as f: 

您正在使用with語句。它需要一個包含__enter____exit__方法的對象。但pattern.findall返回list,with嘗試存儲__exit__方法,但它找不到它,並引發錯誤。只需使用

f = pattern.findall(row) 

改爲。

39

這不是在這種情況下,提問者的問題,但對於一個通用的第一的故障診斷步驟「AttributeError的:__exit__」應確保括號內是有,例如

with SomeEnterExitObject() as foo: 
    #works because a new object is referenced... 

with SomeEnterExitObject as foo: 
    #AttributeError because the class is referenced 

漁獲我出去不時和我在這裏結束了-__-

相關問題