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

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

      1. Python 多处理 - 如何将 kwargs 传递给函数?

        时间:2023-05-25

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

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

                  <tfoot id='m6Jkp'></tfoot>
                  本文介绍了Python 多处理 - 如何将 kwargs 传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  如何使用 Python 的多处理将字典传递给函数?文档:https://docs.python.org/3.4/library/multiprocessing.html#reference 说要传递字典,但我不断得到

                  How do I pass a dictionary to a function with Python's Multiprocessing? The Documentation: https://docs.python.org/3.4/library/multiprocessing.html#reference says to pass a dictionary, but I keep getting

                  TypeError: fp() got multiple values for argument 'what'
                  

                  代码如下:

                  from multiprocessing import Process, Manager
                     
                  def fp(name, numList=None, what='no'):
                          print ('hello %s %s'% (name, what))
                          numList.append(name+'44')
                   
                  if __name__ == '__main__':
                  
                      manager = Manager()
                   
                      numList = manager.list()
                      for i in range(10):
                          keywords = {'what':'yes'}
                          p = Process(target=fp, args=('bob'+str(i)), kwargs={'what':'yes'})
                          p.start()
                          print("Start done")
                          p.join()
                          print("Join done")
                      print (numList)
                  

                  推荐答案

                  当我运行你的代码时,我得到了一个不同的错误:

                  When I ran your code, I got a different error:

                  TypeError: fp() takes at most 3 arguments (5 given)
                  

                  我通过打印 args 和 kwargs 并将方法更改为 fp(*args, **kwargs) 进行了调试,并注意到bob_"被作为一个字母数组传入.用于 args 的括号似乎是可操作的,实际上并没有给你一个元组.将其更改为列表,然后将 numList 作为关键字参数传入,使代码对我有用.

                  I debugged by printing args and kwargs and changing the method to fp(*args, **kwargs) and noticed that "bob_" was being passed in as an array of letters. It seems that the parentheses used for args were operational and not actually giving you a tuple. Changing it to the list, then also passing in numList as a keyword argument, made the code work for me.

                  from multiprocessing import Process, Manager
                  
                  def fp(name, numList=None, what='no'):
                      print ('hello %s %s' % (name, what))
                      numList.append(name+'44')
                  
                  if __name__ == '__main__':
                  
                      manager = Manager()
                  
                      numList = manager.list()
                      for i in range(10):
                          keywords = {'what': 'yes', 'numList': numList}
                          p = Process(target=fp, args=['bob'+str(i)], kwargs=keywords)
                          p.start()
                          print("Start done")
                          p.join()
                          print("Join done")
                      print (numList)
                  

                  这篇关于Python 多处理 - 如何将 kwargs 传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:守护进程内的Python多处理池 下一篇:具有多处理工作人员的扭曲网络客户端?

                  相关文章

                  最新文章

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

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

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