2011-09-16 28 views
61

Tôi đã cố gắng viết lại một số mã csv-đọc để có thể chạy nó trên nhiều lõi trong Python 3.2.2. Tôi đã cố gắng sử dụng đối tượng Pool của đa xử lý, mà tôi thích nghi từ các ví dụ làm việc (và đã làm việc cho tôi cho một phần khác của dự án của tôi). Tôi chạy vào một thông báo lỗi tôi thấy khó giải mã và khắc phục sự cố. Cách tốt để khắc phục sự cố này là gì? Cảm ơn!làm thế nào để khắc phục sự cố "AttributeError: __exit__" trong multiproccesing bằng Python?

Lỗi:

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__ 

Mã liên quan:

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

Trong trường hợp của tôi, tôi đã vô tình đi qua một 'None' do Phạm vi vấn đề. – ThorSummoner

+0

Tôi đã có điều này khi tôi tuyên bố một lớp là 'Class SomeClass (đối tượng): 'mặc dù tôi DID có một __exit__ rõ ràng trong lớp của tôi. Khi tôi đã loại bỏ thừa kế từ 'đối tượng', nó hoạt động. Tôi không biết tại sao, vì vậy YMMV – mpag

Trả lời

111

Vấn đề là ở dòng này:

with pattern.findall(row) as f: 

Bạn đang sử dụng với tuyên bố. Nó đòi hỏi một đối tượng với các phương thức __enter____exit__. Nhưng pattern.findall trả về một số list, with cố gắng lưu trữ phương thức __exit__ nhưng không thể tìm thấy và gây ra lỗi. Chỉ cần sử dụng

f = pattern.findall(row) 

để thay thế.

39

Đây không phải là vấn đề của người hỏi trong trường hợp này, nhưng bước khắc phục sự cố đầu tiên cho một "AttributeError: __exit__" chung phải đảm bảo rằng các dấu ngoặc vuông ở đó, ví dụ:

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

không

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

Catches tôi ra bất cứ lúc nào và tôi kết thúc ở đây -__-

Các vấn đề liên quan