• <small id='LeaZ5'></small><noframes id='LeaZ5'>

      <tfoot id='LeaZ5'></tfoot>
        <legend id='LeaZ5'><style id='LeaZ5'><dir id='LeaZ5'><q id='LeaZ5'></q></dir></style></legend>
          <bdo id='LeaZ5'></bdo><ul id='LeaZ5'></ul>

      1. <i id='LeaZ5'><tr id='LeaZ5'><dt id='LeaZ5'><q id='LeaZ5'><span id='LeaZ5'><b id='LeaZ5'><form id='LeaZ5'><ins id='LeaZ5'></ins><ul id='LeaZ5'></ul><sub id='LeaZ5'></sub></form><legend id='LeaZ5'></legend><bdo id='LeaZ5'><pre id='LeaZ5'><center id='LeaZ5'></center></pre></bdo></b><th id='LeaZ5'></th></span></q></dt></tr></i><div id='LeaZ5'><tfoot id='LeaZ5'></tfoot><dl id='LeaZ5'><fieldset id='LeaZ5'></fieldset></dl></div>
      2. Python 多处理库错误(AttributeError:__exit__)

        时间:2023-05-26
          <tbody id='9ciPH'></tbody>

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

                • <small id='9ciPH'></small><noframes id='9ciPH'>

                  本文介绍了Python 多处理库错误(AttributeError:__exit__)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  使用 pool.map(funct, iterable) 时出现此错误:

                  Am getting this error when using the pool.map(funct, iterable):

                  AttributeError: __exit__
                  

                  没有解释,只是堆栈跟踪到模块内的 pool.py 文件.

                  No Explanation, only stack trace to the pool.py file within the module.

                  这样使用:

                  with Pool(processes=2) as pool:
                     pool.map(myFunction, mylist)
                     pool.map(myfunction2, mylist2)
                  

                  我怀疑picklability可能存在问题(python需要pickle,或将列表数据转换为字节流)但我不确定这是真的还是如何调试.

                  I suspect there could be a problem with the picklability (python needs to pickle, or transform list data into byte stream) yet I'm not sure if this is true or if it is how to debug.

                  产生此错误的新代码格式:

                  new format of code that produces this error :

                  def governingFunct(list):
                      #some tasks
                      def myFunction():
                           # function contents
                      with closing(Pool(processes=2)) as pool:
                           pool.map(myFunction, sublist)
                           pool.map(myFunction2, sublist2)
                  

                  产生错误:

                  PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
                  

                  推荐答案

                  在 Python 2.x 和 3.0、3.1 和 3.2 中,multiprocessing.Pool() 对象不是上下文管理器.您不能在 with 语句中使用它们.只有在 Python 3.3 及更高版本中,您才能使用它们.来自 Python 3 multiprocessing.Pool() 文档:

                  In Python 2.x and 3.0, 3.1 and 3.2, multiprocessing.Pool() objects are not context managers. You cannot use them in a with statement. Only in Python 3.3 and up can you use them as such. From the Python 3 multiprocessing.Pool() documentation:

                  3.3 版中的新功能:池对象现在支持上下文管理协议 - 请参阅上下文管理器类型.__enter__() 返回池对象,__exit__() 调用 terminate().

                  New in version 3.3: Pool objects now support the context management protocol – see Context Manager Types. __enter__() returns the pool object, and __exit__() calls terminate().

                  对于早期的 Python 版本,您可以使用 contextlib.closing(),但考虑到这将调用 pool.close(),而不是 pool.terminate().在这种情况下手动终止:

                  For earlier Python versions, you could use contextlib.closing(), but take into account this'll call pool.close(), not pool.terminate(). Terminate manually in that case:

                  from contextlib import closing
                  
                  with closing(Pool(processes=2)) as pool:
                      pool.map(myFunction, mylist)
                      pool.map(myfunction2, mylist2)
                      pool.terminate()
                  

                  或创建自己的 terminating() 上下文管理器:

                  or create your own terminating() context manager:

                  from contextlib import contextmanager
                  
                  @contextmanager
                  def terminating(thing):
                      try:
                          yield thing
                      finally:
                          thing.terminate()
                  
                  with terminating(Pool(processes=2)) as pool:
                      pool.map(myFunction, mylist)
                      pool.map(myfunction2, mylist2)
                  

                  这篇关于Python 多处理库错误(AttributeError:__exit__)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“块大小"multiprocessing.Pool.map 中的参数 下一篇:如何让 Pool.map 采用 lambda 函数

                  相关文章

                  最新文章

                • <small id='Eggor'></small><noframes id='Eggor'>

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