python - 如何在 Python 中对多处理中的 "AttributeError: __exit__"进行故障排除?

标签 python python-3.x multiprocessing pool

我尝试重写一些 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)

解决此问题的好方法是什么?

最佳答案

问题出在这一行:

with pattern.findall(row) as f:

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

f = pattern.findall(row)

改为。

关于python - 如何在 Python 中对多处理中的 "AttributeError: __exit__"进行故障排除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7447284/

相关文章:

Python 用户输入方程

python - n 树的副本

python - 如何在 Python 3 中解析原始 HTTP 请求?

python - 如何在导入 win32api 时修复 "ImportError: DLL load failed"

python - multiprocessing 返回 "too many open files"但使用 `with...as` 修复它。为什么?

Python multiprocessing.Process 对象的行为就像它持有对另一个进程中的对象的引用。为什么?

python - 已注册的 atexit 处理程序是否由派生的子进程继承?

python - Python中的NameError,名称未定义

python - 如何在 python 中围绕解析函数的闭包中创建索引

python-3.x - Discord.py 重写Modmail系统