• <legend id='9UNN4'><style id='9UNN4'><dir id='9UNN4'><q id='9UNN4'></q></dir></style></legend>

      <bdo id='9UNN4'></bdo><ul id='9UNN4'></ul>

      1. <small id='9UNN4'></small><noframes id='9UNN4'>

      2. <tfoot id='9UNN4'></tfoot>
        <i id='9UNN4'><tr id='9UNN4'><dt id='9UNN4'><q id='9UNN4'><span id='9UNN4'><b id='9UNN4'><form id='9UNN4'><ins id='9UNN4'></ins><ul id='9UNN4'></ul><sub id='9UNN4'></sub></form><legend id='9UNN4'></legend><bdo id='9UNN4'><pre id='9UNN4'><center id='9UNN4'></center></pre></bdo></b><th id='9UNN4'></th></span></q></dt></tr></i><div id='9UNN4'><tfoot id='9UNN4'></tfoot><dl id='9UNN4'><fieldset id='9UNN4'></fieldset></dl></div>

      3. 如何对“AttributeError: __exit__"进行故障排除在

        时间:2023-05-27

          <small id='szoFN'></small><noframes id='szoFN'>

            <bdo id='szoFN'></bdo><ul id='szoFN'></ul>
            <i id='szoFN'><tr id='szoFN'><dt id='szoFN'><q id='szoFN'><span id='szoFN'><b id='szoFN'><form id='szoFN'><ins id='szoFN'></ins><ul id='szoFN'></ul><sub id='szoFN'></sub></form><legend id='szoFN'></legend><bdo id='szoFN'><pre id='szoFN'><center id='szoFN'></center></pre></bdo></b><th id='szoFN'></th></span></q></dt></tr></i><div id='szoFN'><tfoot id='szoFN'></tfoot><dl id='szoFN'><fieldset id='szoFN'></fieldset></dl></div>
              • <tfoot id='szoFN'></tfoot>
                • <legend id='szoFN'><style id='szoFN'><dir id='szoFN'><q id='szoFN'></q></dir></style></legend>
                    <tbody id='szoFN'></tbody>
                  本文介绍了如何对“AttributeError: __exit__"进行故障排除在 Python 中进行多处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

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

                  I tried to rewrite some csv-reading code to be able to run it on multiple cores in Python 3.2.2. I tried to use the Pool object of multiprocessing, which I adapted from working examples (and already worked for me for another part of my project). I ran into an error message I found hard to decipher and troubleshoot.

                  错误:

                  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"/]+?(?=[,
                  ])""")
                      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)
                  

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

                  What's a good way to troubleshoot this?

                  推荐答案

                  问题出在这一行:

                  with pattern.findall(row) as f:
                  

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

                  You are using the with statement. It requires an object with __enter__ and __exit__ methods. But pattern.findall returns a list, with tries to store the __exit__ method, but it can't find it, and raises an error. Just use

                  f = pattern.findall(row)
                  

                  改为.

                  这篇关于如何对“AttributeError: __exit__"进行故障排除在 Python 中进行多处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:带有悲情的 Python 多处理 下一篇:使用多处理写入文件

                  相关文章

                  最新文章

                  1. <tfoot id='zi5E6'></tfoot>

                    <small id='zi5E6'></small><noframes id='zi5E6'>

                    1. <legend id='zi5E6'><style id='zi5E6'><dir id='zi5E6'><q id='zi5E6'></q></dir></style></legend>
                        <bdo id='zi5E6'></bdo><ul id='zi5E6'></ul>

                      <i id='zi5E6'><tr id='zi5E6'><dt id='zi5E6'><q id='zi5E6'><span id='zi5E6'><b id='zi5E6'><form id='zi5E6'><ins id='zi5E6'></ins><ul id='zi5E6'></ul><sub id='zi5E6'></sub></form><legend id='zi5E6'></legend><bdo id='zi5E6'><pre id='zi5E6'><center id='zi5E6'></center></pre></bdo></b><th id='zi5E6'></th></span></q></dt></tr></i><div id='zi5E6'><tfoot id='zi5E6'></tfoot><dl id='zi5E6'><fieldset id='zi5E6'></fieldset></dl></div>