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

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

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

        • <bdo id='B7CSG'></bdo><ul id='B7CSG'></ul>
      1. <tfoot id='B7CSG'></tfoot>

        在 Python 中将多个参数传递给 pool.map() 函数

        时间:2023-05-27
            <legend id='LG6GR'><style id='LG6GR'><dir id='LG6GR'><q id='LG6GR'></q></dir></style></legend>
              <tbody id='LG6GR'></tbody>

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

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

                <bdo id='LG6GR'></bdo><ul id='LG6GR'></ul>
                <tfoot id='LG6GR'></tfoot>

                  本文介绍了在 Python 中将多个参数传递给 pool.map() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我需要一些方法来使用 pool.map() 中接受多个参数的函数.根据我的理解, pool.map() 的目标函数只能有一个可迭代的参数,但是有没有一种方法可以传递其他参数?在这种情况下,我需要传入一些配置变量,例如我的 Lock() 和日志信息到目标函数.

                  I need some way to use a function within pool.map() that accepts more than one parameter. As per my understanding, the target function of pool.map() can only have one iterable as a parameter but is there a way that I can pass other parameters in as well? In this case, I need to pass in a few configuration variables, like my Lock() and logging information to the target function.

                  我试图做一些研究,我认为我可以使用部分函数来让它工作?但是我不完全理解这些是如何工作的.任何帮助将不胜感激!这是我想做的一个简单示例:

                  I have tried to do some research and I think that I may be able to use partial functions to get it to work? However I don't fully understand how these work. Any help would be greatly appreciated! Here is a simple example of what I want to do:

                  def target(items, lock):
                      for item in items:
                          # Do cool stuff
                          if (... some condition here ...):
                              lock.acquire()
                              # Write to stdout or logfile, etc.
                              lock.release()
                  
                  def main():
                      iterable = [1, 2, 3, 4, 5]
                      pool = multiprocessing.Pool()
                      pool.map(target(PASS PARAMS HERE), iterable)
                      pool.close()
                      pool.join()
                  

                  推荐答案

                  你可以使用functools.partial 为此(正如您所怀疑的那样):

                  You can use functools.partial for this (as you suspected):

                  from functools import partial
                  
                  def target(lock, iterable_item):
                      for item in iterable_item:
                          # Do cool stuff
                          if (... some condition here ...):
                              lock.acquire()
                              # Write to stdout or logfile, etc.
                              lock.release()
                  
                  def main():
                      iterable = [1, 2, 3, 4, 5]
                      pool = multiprocessing.Pool()
                      l = multiprocessing.Lock()
                      func = partial(target, l)
                      pool.map(func, iterable)
                      pool.close()
                      pool.join()
                  

                  例子:

                  def f(a, b, c):
                      print("{} {} {}".format(a, b, c))
                  
                  def main():
                      iterable = [1, 2, 3, 4, 5]
                      pool = multiprocessing.Pool()
                      a = "hi"
                      b = "there"
                      func = partial(f, a, b)
                      pool.map(func, iterable)
                      pool.close()
                      pool.join()
                  
                  if __name__ == "__main__":
                      main()
                  

                  输出:

                  hi there 1
                  hi there 2
                  hi there 3
                  hi there 4
                  hi there 5
                  

                  这篇关于在 Python 中将多个参数传递给 pool.map() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:multiprocessing.pool.MaybeEncodingError: 'TypeError(" 下一篇:Python 多处理模块的 .join() 方法到底在做什么?

                  相关文章

                  最新文章

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

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

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

                    <tfoot id='z02nR'></tfoot>