久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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 class="qwawimqqmiuu" id='myjrt'><tfoot id='myjrt'></tfoot><dl id='myjrt'><fieldset id='myjrt'></fieldset></dl></div>
        • <bdo id='myjrt'></bdo><ul id='myjrt'></ul>
        <legend id='myjrt'><style id='myjrt'><dir id='myjrt'><q id='myjrt'></q></dir></style></legend>
      1. Python Multiprocessing - 將類方法應用于對象列表

        Python Multiprocessing - apply class method to a list of objects(Python Multiprocessing - 將類方法應用于對象列表)
        <tfoot id='MLMUv'></tfoot>

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

              • <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 class="qwawimqqmiuu" 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>
                  本文介紹了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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數傳遞給 pool.map() 函數)
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)
                    <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>
                              <tbody id='YzTtP'></tbody>
                          • <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 class="qwawimqqmiuu" id='YzTtP'><tfoot id='YzTtP'></tfoot><dl id='YzTtP'><fieldset id='YzTtP'></fieldset></dl></div>
                            主站蜘蛛池模板: www.国产精品| 日本午夜在线视频 | 久久精品二区 | 日韩免费1区二区电影 | 久久日韩精品一区二区三区 | 国产精品久久久久久高潮 | 久久精品亚洲精品国产欧美kt∨ | 精品欧美一区二区三区久久久 | va在线 | 久久日韩精品 | 黄色片网站在线观看 | 国产成人综合一区二区三区 | 六月成人网 | 少妇一级淫片免费放播放 | 精品视频一区在线 | 91精品国产乱码久久久久久久久 | 日韩中文一区二区三区 | 国产最新精品视频 | 国产成人精品久久二区二区91 | 欧美日韩在线一区二区 | 久久国产精品一区 | 视频一区中文字幕 | 久久久久国产一区二区三区四区 | 狠狠久久| 一区二区三区高清在线观看 | 欧美日韩国产一区 | 99re视频精品| 久久草在线视频 | 成人一区在线观看 | 少妇一级淫片免费播放 | 久久精品一区二区视频 | 久久久99精品免费观看 | av看片网站| 国产黄色在线观看 | 久久久成人免费视频 | 国产精品一级 | 日韩免费视频 | 国产精品久久久久久久久久久免费看 | 午夜精品久久久久久久久久久久久 | 精品av| 成人亚洲在线 |