<bdo id='g4fNL'></bdo><ul id='g4fNL'></ul>

    1. <legend id='g4fNL'><style id='g4fNL'><dir id='g4fNL'><q id='g4fNL'></q></dir></style></legend>
      <tfoot id='g4fNL'></tfoot>

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

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

        如何将生成器用作具有多处理映射功能的可迭代

        时间:2023-05-27
          <tfoot id='rv465'></tfoot>
            <bdo id='rv465'></bdo><ul id='rv465'></ul>
          • <legend id='rv465'><style id='rv465'><dir id='rv465'><q id='rv465'></q></dir></style></legend>

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

                <tbody id='rv465'></tbody>

                  <i id='rv465'><tr id='rv465'><dt id='rv465'><q id='rv465'><span id='rv465'><b id='rv465'><form id='rv465'><ins id='rv465'></ins><ul id='rv465'></ul><sub id='rv465'></sub></form><legend id='rv465'></legend><bdo id='rv465'><pre id='rv465'><center id='rv465'></center></pre></bdo></b><th id='rv465'></th></span></q></dt></tr></i><div id='rv465'><tfoot id='rv465'></tfoot><dl id='rv465'><fieldset id='rv465'></fieldset></dl></div>
                  本文介绍了如何将生成器用作具有多处理映射功能的可迭代对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  当我使用生成器作为带有 multiprocessing.Pool.map 函数的可迭代参数时:

                  When I use a generator as an iterable argument with multiprocessing.Pool.map function:

                  pool.map(func, iterable=(x for x in range(10)))
                  

                  func 被调用之前,生成器似乎已经完全耗尽.

                  It seems that the generator is fully exhausted before func is ever called.

                  我想生成每个项目并将其传递给每个进程,谢谢

                  I want to yield each item and pass it to each process, thanks

                  推荐答案

                  multiprocessing.map 在处理之前将没有 __len__ 方法的可迭代对象转换为列表.这样做是为了帮助计算块大小,池使用它来对工作参数进行分组并降低调度作业的往返成本.这不是最优的,尤其是当 chunksize 为 1 时,但由于 map 必须以一种或另一种方式耗尽迭代器,它通常不是一个重大问题.

                  multiprocessing.map converts iterables without a __len__ method to a list before processing. This is done to aid the calculation of chunksize, which the pool uses to group worker arguments and reduce the round trip cost of scheduling jobs. This is not optimal, especially when chunksize is 1, but since map must exhaust the iterator one way or the other, its usually not a significant issue.

                  相关代码在pool.py中.注意它对 len 的使用:

                  The relevant code is in pool.py. Notice its use of len:

                  def _map_async(self, func, iterable, mapper, chunksize=None, callback=None,
                          error_callback=None):
                      '''
                      Helper function to implement map, starmap and their async counterparts.
                      '''
                      if self._state != RUN:
                          raise ValueError("Pool not running")
                      if not hasattr(iterable, '__len__'):
                          iterable = list(iterable)
                  
                      if chunksize is None:
                          chunksize, extra = divmod(len(iterable), len(self._pool) * 4)
                          if extra:
                              chunksize += 1
                      if len(iterable) == 0:
                          chunksize = 0
                  

                  这篇关于如何将生成器用作具有多处理映射功能的可迭代对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用多处理读取多个文件 下一篇:Windows 上的多处理中断

                  相关文章

                  最新文章

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

                    • <bdo id='9HlOQ'></bdo><ul id='9HlOQ'></ul>

                    <small id='9HlOQ'></small><noframes id='9HlOQ'>