如何在Python多进程中解决“ AttributeError:__ exit__”问题?


87

我试图重写一些csv读取代码,以便能够在Python 3.2.2中的多个内核上运行它。我尝试使用Pool多重处理的对象,该对象是根据工作示例改编而成的(并且已经为我的项目的另一部分工作了)。我遇到了一条错误消息,发现很难破译和排除故障。

错误:

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由于范围问题,我不小心通过了a 。
ThorSummoner '16

Class SomeClass(object):即使我DID 在类中显式退出,我在声明类时也遇到了这个问题。一旦我从object它删除继承。我不知道为什么,所以YMMV
mpag

Answers:


154

问题在这一行:

with pattern.findall(row) as f:

您正在使用该with语句。它需要具有__enter____exit__方法的对象。但是pattern.findall返回一个listwith尝试存储该__exit__方法,但找不到该方法,并引发错误。只需使用

f = pattern.findall(row)

代替。


62

在这种情况下,这不是提问者的问题,但是对于常规“ AttributeError:__ exit__” 的第一步故障排除步骤应该是确保括号在那里,例如

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

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

有时会把我赶出去,我到这里去-__-



-1

该错误背后的原因是:Flask应用程序已在运行,尚未关闭,在此过程中,我们尝试通过以下方式启动另一个实例:with app.app_context():#Code在将其与语句一起使用之前,我们需要进行以下操作:确保关闭了先前正在运行的应用程序的范围。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.