<tfoot id='MLMUv'></tfoot>

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

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

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

          <bdo id='MLMUv'></bdo><ul id='MLMUv'></ul>
      2. Python Multiprocessing - 将类方法应用于对象列表

        时间:2023-05-26
      3. <tfoot id='myjrt'></tfoot>

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

        <i id='myjrt'><tr id='myjrt'><dt id='myjrt'><q id='myjrt'><span id='myjrt'><b id='myjrt'><form id='myjrt'><ins id='myjrt'></ins><ul id='myjrt'></ul><sub id='myjrt'></sub></form><legend id='myjrt'></legend><bdo id='myjrt'><pre id='myjrt'><center id='myjrt'></center></pre></bdo></b><th id='myjrt'></th></span></q></dt></tr></i><div id='myjrt'><tfoot id='myjrt'></tfoot><dl id='myjrt'><fieldset id='myjrt'></fieldset></dl></div>
                <tbody id='myjrt'></tbody>
              • <bdo id='myjrt'></bdo><ul id='myjrt'></ul>
                <legend id='myjrt'><style id='myjrt'><dir id='myjrt'><q id='myjrt'></q></dir></style></legend>
                • 本文介绍了Python Multiprocessing - 将类方法应用于对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  有没有一种简单的方法可以使用多处理来完成此操作?

                  Is there a simple way to use Multiprocessing to do the equivalent of this?

                  for sim in sim_list:
                    sim.run()
                  

                  其中 sim_list 的元素是模拟"对象,而 run() 是模拟类的一个方法,它确实修改对象的属性.例如:

                  where the elements of sim_list are "simulation" objects and run() is a method of the simulation class which does modify the attributes of the objects. E.g.:

                  class simulation:
                      def __init__(self):
                          self.state['done']=False
                          self.cmd="program"
                      def run(self):
                          subprocess.call(self.cmd)
                          self.state['done']=True
                  

                  sim_list 中的所有 sim 都是独立的,因此策略不必是线程安全的.

                  All the sim in sim_list are independent, so the strategy does not have to be thread safe.

                  我尝试了以下,这显然是有缺陷的,因为参数是通过 deepcopy 传递的,并且没有就地修改.

                  I tried the following, which is obviously flawed because the argument is passed by deepcopy and is not modified in-place.

                  from multiprocessing import Process
                  
                  for sim in sim_list:
                    b = Process(target=simulation.run, args=[sim])
                    b.start()
                    b.join()
                  

                  推荐答案

                  做你想做的事情的一种方法是让你的计算类(simulation 在你的例子中)成为 的子类处理.正确初始化后,此类的实例将在单独的进程中运行,您可以根据需要从列表中设置一组实例.

                  One way to do what you want is to have your computing class (simulation in your case) be a subclass of Process. When initialized properly, instances of this class will run in separate processes and you can set off a group of them from a list just like you wanted.

                  这是一个示例,基于您上面写的内容:

                  Here's an example, building on what you wrote above:

                  import multiprocessing
                  import os
                  import random
                  
                  class simulation(multiprocessing.Process):
                      def __init__(self, name):
                          # must call this before anything else
                          multiprocessing.Process.__init__(self)
                  
                          # then any other initialization
                          self.name = name
                          self.number = 0.0
                          sys.stdout.write('[%s] created: %f
                  ' % (self.name, self.number))
                  
                      def run(self):
                          sys.stdout.write('[%s] running ...  process id: %s
                  ' 
                                           % (self.name, os.getpid()))
                  
                          self.number = random.uniform(0.0, 10.0)
                          sys.stdout.write('[%s] completed: %f
                  ' % (self.name, self.number))
                  

                  然后只需制作一个对象列表并以循环开始每个对象:

                  Then just make a list of objects and start each one with a loop:

                  sim_list = []
                  sim_list.append(simulation('foo'))
                  sim_list.append(simulation('bar'))
                  
                  for sim in sim_list:
                      sim.start()
                  

                  当您运行它时,您应该会看到每个对象都在其自己的进程中运行.不要忘记调用 Process.__init__(self) 作为类初始化中的第一件事,然后再进行其他操作.

                  When you run this you should see each object run in its own process. Don't forget to call Process.__init__(self) as the very first thing in your class initialization, before anything else.

                  显然我没有在这个例子中包含任何进程间通信;如果您的情况需要,您必须添加它(从您的问题中不清楚您是否需要它).

                  Obviously I've not included any interprocess communication in this example; you'll have to add that if your situation requires it (it wasn't clear from your question whether you needed it or not).

                  这种方法对我很有效,我不知道有什么缺点.如果有人知道我忽略的隐患,请告诉我.

                  This approach works well for me, and I'm not aware of any drawbacks. If anyone knows of hidden dangers which I've overlooked, please let me know.

                  我希望这会有所帮助.

                  这篇关于Python Multiprocessing - 将类方法应用于对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Python 中使用 Managers() 在多个进程之间共享 下一篇:使用 Python 多处理管理器 (BaseManager/SyncManager) 与远

                  相关文章

                  最新文章

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

                    <tfoot id='YzTtP'></tfoot>

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

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