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

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

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

        • <bdo id='ZbaBz'></bdo><ul id='ZbaBz'></ul>

        類方法的并行執行

        Parallel execution of class methods(類方法的并行執行)

          • <bdo id='7fGZs'></bdo><ul id='7fGZs'></ul>

          • <i id='7fGZs'><tr id='7fGZs'><dt id='7fGZs'><q id='7fGZs'><span id='7fGZs'><b id='7fGZs'><form id='7fGZs'><ins id='7fGZs'></ins><ul id='7fGZs'></ul><sub id='7fGZs'></sub></form><legend id='7fGZs'></legend><bdo id='7fGZs'><pre id='7fGZs'><center id='7fGZs'></center></pre></bdo></b><th id='7fGZs'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='7fGZs'><tfoot id='7fGZs'></tfoot><dl id='7fGZs'><fieldset id='7fGZs'></fieldset></dl></div>

            <tfoot id='7fGZs'></tfoot>
            • <small id='7fGZs'></small><noframes id='7fGZs'>

                    <tbody id='7fGZs'></tbody>
                  <legend id='7fGZs'><style id='7fGZs'><dir id='7fGZs'><q id='7fGZs'></q></dir></style></legend>
                  本文介紹了類方法的并行執行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我需要并行執行同一類的許多實例的方法.為此,我嘗試使用 Process.start()Process.join() 命令來自 multiprocessing 模塊.

                  I need to execute in parallel a method of many instances of the same class. For doing this I'm trying to use the Process.start() and the Process.join() commands from the multiprocessing module.

                  例如對于一個類:

                  class test:
                       def __init__(self):
                       ...
                       ...
                       def method(self):
                       ...
                       ...
                  

                  where method 修改了一些類變量.如果我創建該類的兩個實例:

                  where method modifies some of the class variables. If I make two instances of the class:

                  t1=test()
                  t2=test()
                  

                  并執行:

                  from multiprocessing import Process
                  pr1=Process(target=t1.method, args=(,))
                  pr2=Process(target=t2.method, args=(,))
                  pr1.start()
                  pr2.start()
                  pr1.join()
                  pr2.join()
                  

                  類實例的變量沒有更新(整個代碼太長,這里貼不上來,不過是這個思路).

                  the variables of the instances of the class are not updated (the whole code is too long to be pasted here but this is the idea).

                  有什么方法可以實現嗎?謝謝

                  Is there any way to achieve this? Thank you

                  推薦答案

                  當您在子進程中調用 obj.method 時,子進程將在 <代碼>對象.因此,您在子項中對它們所做的更改不會反映在父項中.您需要通過 multiprocessing.Queue 以使更改生效父:

                  When you call obj.method in a child process, the child process is getting its own separate copy of each instance variable in obj. So, the changes you make to them in the child will not be reflected in the parent. You'll need to explicitly pass the changed values back to the parent via a multiprocessing.Queue in order to make the changes take effect the parent:

                  from multiprocessing import Process, Queue
                  q1 = Queue()
                  q2 = Queue()
                  pr1 = Process(target=t1.method, args=(q1,))
                  pr2 = Process(target=t2.method, args=(q2,))
                  pr1.start()
                  pr2.start()
                  out1 = q1.get()
                  out2 = q2.get()
                  t1.blah = out1
                  t2.blah = out2
                  pr1.join()
                  pr2.join()
                  

                  其他選項是使您需要更改的實例變量 multiprocessing.Value 實例,或 multiprocessing.Manager Proxy 實例.這樣,您在子項中所做的更改會自動反映在父項中.但這是以增加使用父級變量的開銷為代價的.

                  Other options would be to make the instance variables you need to change multiprocessing.Value instances, or multiprocessing.Manager Proxy instances. That way, the changes you make in the children would be reflected in the parent automatically. But that comes at the cost of adding overhead to using the variables in the parent.

                  這是一個使用 multiprocessing.Manager 的示例.這不起作用:

                  Here's an example using multiprocessing.Manager. This doesn't work:

                  import multiprocessing
                  
                  class Test(object) :
                  
                      def __init__(self):
                         self.some_list = []  # Normal list
                  
                      def method(self):
                          self.some_list.append(123)  # This change gets lost
                  
                  
                  if __name__ == "__main__":
                      t1 = Test()
                      t2 = Test()
                      pr1 = multiprocessing.Process(target=t1.method)
                      pr2 = multiprocessing.Process(target=t2.method)
                      pr1.start()
                      pr2.start()
                      pr1.join()
                      pr2.join()
                      print(t1.some_list)
                      print(t2.some_list)
                  

                  輸出:

                  []
                  []
                  

                  這行得通:

                  import multiprocessing
                  
                  class Test(object) :
                  
                      def __init__(self):
                         self.manager = multiprocessing.Manager()
                         self.some_list = self.manager.list()  # Shared Proxy to a list
                  
                      def method(self):
                          self.some_list.append(123) # This change won't be lost
                  
                  
                  if __name__ == "__main__":
                      t1 = Test()
                      t2 = Test()
                      pr1 = multiprocessing.Process(target=t1.method)
                      pr2 = multiprocessing.Process(target=t2.method)
                      pr1.start()
                      pr2.start()
                      pr1.join()
                      pr2.join()
                      print(t1.some_list)
                      print(t2.some_list)
                  

                  輸出:

                  [123]
                  [123]
                  

                  請記住,multiprocessing.Manager 會啟動一個子進程來管理您創建的所有共享實例,并且每次您訪問其中一個 Proxy 實例時,您實際上是在對 Manager 進程進行 IPC 調用.

                  Just keep in mind that a multiprocessing.Manager starts a child process to manage all the shared instances you create, and that every time you access one of the Proxy instances, you're actually making an IPC call to the Manager process.

                  這篇關于類方法的并行執行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)

                    • <bdo id='XUelb'></bdo><ul id='XUelb'></ul>

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

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

                            <tbody id='XUelb'></tbody>

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

                            主站蜘蛛池模板: 亚洲精品一区二区在线观看 | 欧美激情精品久久久久 | 一区二区三区在线看 | 黄色片a级 | 久久久久久99 | 久草视频在线播放 | 久久精品久久久久久 | 亚洲精品久久久久久久久久久久久 | 亚洲啪啪一区 | 九九精品在线 | 精品三级在线观看 | av手机在线播放 | 一区在线视频 | 日韩在线视频免费观看 | 久久国产精品无码网站 | 欧美精品片 | 性高湖久久久久久久久 | 天天操夜夜操 | 国产精品美女一区二区三区 | 久久免费资源 | 91在线观看免费视频 | 日韩欧美不卡 | 一区日韩| 欧美一区二区三区电影 | 国产一区| 国产精品一区在线 | 成人久久久 | 精品日韩一区 | 黄a网| 午夜码电影| 久久婷婷av| 一级a爱片久久毛片 | 欧美一区精品 | 91福利在线导航 | 91传媒在线观看 | 久久久久久久久99 | 免费精品视频在线观看 | www.久久| 欧美一区二区在线免费观看 | 亚洲看片网站 | 国产乱码精品一区二区三区忘忧草 |