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

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

        <legend id='E39k9'><style id='E39k9'><dir id='E39k9'><q id='E39k9'></q></dir></style></legend>

        multiprocessing.pool.MaybeEncodingError: 'TypeError("

        时间:2023-05-27

          <tbody id='Z4WFF'></tbody>

            1. <small id='Z4WFF'></small><noframes id='Z4WFF'>

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

                • <bdo id='Z4WFF'></bdo><ul id='Z4WFF'></ul>
                  本文介绍了multiprocessing.pool.MaybeEncodingError: 'TypeError("cannot serialize '_io.BufferedReader' object",)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  为什么下面的代码只适用于multiprocessing.dummy,而不适用于简单的multiprocessing.

                  Why does the code below work only with multiprocessing.dummy, but not with simple multiprocessing.

                  import urllib.request
                  #from multiprocessing.dummy import Pool #this works
                  from multiprocessing import Pool
                  
                  urls = ['http://www.python.org', 'http://www.yahoo.com','http://www.scala.org', 'http://www.google.com']
                  
                  if __name__ == '__main__':
                      with Pool(5) as p:
                          results = p.map(urllib.request.urlopen, urls)
                  

                  错误:

                  Traceback (most recent call last):
                    File "urlthreads.py", line 31, in <module>
                      results = p.map(urllib.request.urlopen, urls)
                    File "C:UserspatriAnaconda3libmultiprocessingpool.py", line 268, in map
                      return self._map_async(func, iterable, mapstar, chunksize).get()
                    File "C:UserspatriAnaconda3libmultiprocessingpool.py", line 657, in get
                      raise self._value
                  multiprocessing.pool.MaybeEncodingError: Error sending result: '[<http.client.HTTPResponse object at 0x0000016AEF204198>]'. Reason: 'TypeError("cannot serialize '_io.BufferedReader' object")'
                  

                  缺少什么才能在没有虚拟"的情况下工作?

                  What's missing so that it works without "dummy" ?

                  推荐答案

                  你从 urlopen() 得到的 http.client.HTTPResponse-object 有一个 >_io.BufferedReader - 附加对象,这个对象不能被pickle.

                  The http.client.HTTPResponse-object you get back from urlopen() has a _io.BufferedReader-object attached, and this object cannot be pickled.

                  pickle.dumps(urllib.request.urlopen('http://www.python.org').fp)
                  Traceback (most recent call last):
                  ...
                      pickle.dumps(urllib.request.urlopen('http://www.python.org').fp)
                  TypeError: cannot serialize '_io.BufferedReader' object
                  

                  multiprocessing.Pool 将需要腌制(序列化)结果以将其发送回父进程,但此处失败.由于 dummy 使用线程而不是进程,因此不会出现酸洗,因为同一进程中的线程自然共享它们的内存.

                  multiprocessing.Pool will need to pickle (serialize) the results to send it back to the parent process and this fails here. Since dummy uses threads instead of processes, there will be no pickling, because threads in the same process share their memory naturally.

                  这个TypeError的一般解决方案是:

                  A general solution to this TypeError is:

                  1. 读出缓冲区并保存内容(如果需要)
                  2. 从您尝试腌制的对象中删除对 '_io.BufferedReader' 的引用

                  在您的情况下,在 http.client.HTTPResponse 上调用 .read() 将清空并删除缓冲区,因此是用于将响应转换为可腌制内容的函数可以这样做:

                  In your case, calling .read() on the http.client.HTTPResponse will empty and remove the buffer, so a function for converting the response into something pickleable could simply do this:

                  def read_buffer(response):
                      response.text = response.read()
                      return response
                  

                  例子:

                  r = urllib.request.urlopen('http://www.python.org')
                  r = read_buffer(r)
                  pickle.dumps(r)
                  # Out: b'x80x03chttp.client
                  HTTPResponse...
                  

                  在考虑这种方法之前,请确保您确实想要使用多处理而不是多线程.对于像您在此处拥有的 I/O 绑定任务,多线程就足够了,因为无论如何大部分时间都花在等待响应上(不需要 cpu 时间).多处理和所涉及的 IPC 也会带来大量开销.

                  Before you consider this approach, make sure you really want to use multiprocessing instead of multithreading. For I/O-bound tasks like you have it here, multithreading would be sufficient, since most of the time is spend in waiting (no need for cpu-time) for the response anyway. Multiprocessing and the IPC involved also introduces substantial overhead.

                  这篇关于multiprocessing.pool.MaybeEncodingError: 'TypeError("cannot serialize '_io.BufferedReader' object",)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 多进程池.当其中一个工作进程确定不再需要 下一篇:在 Python 中将多个参数传递给 pool.map() 函数

                  相关文章

                  最新文章

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

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

                    <legend id='EpsF2'><style id='EpsF2'><dir id='EpsF2'><q id='EpsF2'></q></dir></style></legend>