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

    <small id='7CnnB'></small><noframes id='7CnnB'>

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

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

      Python多處理,傳遞包含信號(hào)量的對(duì)象引用

      Python multiprocessing, passing an object reference containig a semaphore(Python多處理,傳遞包含信號(hào)量的對(duì)象引用)
        <legend id='Xatwo'><style id='Xatwo'><dir id='Xatwo'><q id='Xatwo'></q></dir></style></legend>

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

        <tfoot id='Xatwo'></tfoot>

            <tbody id='Xatwo'></tbody>

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

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

                本文介紹了Python多處理,傳遞包含信號(hào)量的對(duì)象引用的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我有這樣的場景:我創(chuàng)建了一個(gè)包含信號(hào)量的類元素的對(duì)象.

                import multiprocessing as mpr類元素(對(duì)象):def __init__(self):self.sem = mpr.Semaphore()自我.xyz = 33定義樂趣(ch):a = ch.recv()打印(一個(gè)[0])打印(a[1].xyz)a[1].xyz = 99打印(a[1].xyz)el = 元素()( pa , ch ) = mpr.Pipe()proc = mpr.Process(target=fun, args=( ch, ) )proc.start()pa.send([你好", el ])打印(el.xyz)proc.join()

                此代碼返回此錯(cuò)誤:

                 文件/usr/lib/python2.7/multiprocessing/forking.py",第 51 行,在 assert_spawning 中'通過繼承' % type(self).__name__RuntimeError:信號(hào)量對(duì)象只能通過繼承在進(jìn)程之間共享

                但是如果我從 Element 的聲明中刪除信號(hào)量,代碼可以工作,但是分配給 a[1].xyz 的值將會(huì)丟失.

                現(xiàn)在我需要通過信號(hào)量和多處理同步大量對(duì)象.那么有一些方法可以在每個(gè)對(duì)象中設(shè)置一個(gè)信號(hào)量并只傳遞對(duì)主對(duì)象的引用嗎?

                import multiprocessing as mpr類元素(對(duì)象):def __init__(self):自我.xyz = 33定義樂趣(ch):a = ch.recv()打印(一個(gè)[0])打印(a[1].xyz)a[1].xyz = 99打印(a[1].xyz)el = 元素()( pa , ch ) = mpr.Pipe()proc = mpr.Process(target=fun, args=( ch, ) )proc.start()pa.send([你好", el ])打印(el.xyz)proc.join()

                第二個(gè)版本不會(huì)產(chǎn)生任何錯(cuò)誤,但是分配給a[1].xyz = 99的值會(huì)在主進(jìn)程中丟失.

                解決方案

                我認(rèn)為您不了解 multiprocessing 模塊的工作原理.

                當(dāng)您通過管道發(fā)送某些內(nèi)容時(shí),它會(huì)在子流程中被腌制然后解除腌制.這意味著子進(jìn)程實(shí)際上有一個(gè)原始對(duì)象的副本!這就是改變丟失"的原因.添加信號(hào)量不會(huì)改變?nèi)魏螙|西.

                如果您想要共享內(nèi)存中的對(duì)象,您應(yīng)該使用 multiprocessing.Value,即使這不處理任意類型.可能 multiprocessing.Manager 是您正在尋找的.

                另一種方法是向提供修改對(duì)象的主進(jìn)程發(fā)送響應(yīng).

                I've a scenario like this: I've created an object of the class element containing a semaphore.

                import multiprocessing as mpr
                
                class Element(object):
                    def __init__(self):
                        self.sem = mpr.Semaphore()
                        self.xyz = 33
                
                def fun( ch ):
                    a = ch.recv()
                    print( a[0] )
                    print( a[1].xyz )
                    a[1].xyz = 99
                    print( a[1].xyz )
                
                
                el = Element()
                
                ( pa , ch ) = mpr.Pipe()
                proc = mpr.Process(target=fun , args=( ch, ) )
                
                proc.start()
                pa.send( [ "Hallo" , el ])
                
                print( el.xyz )
                
                proc.join()
                

                This code return this error:

                  File "/usr/lib/python2.7/multiprocessing/forking.py", line 51, in assert_spawning
                    ' through inheritance' % type(self).__name__
                RuntimeError: Semaphore objects should only be shared between processes through inheritance
                

                But if I remove the semaphore from the declaration of Element the code works, but the value assigned to a[1].xyz will be lost.

                Now I need to synchronizes a big collection of object via semphore and multiprocessing. So there's some method for setting a semaphore in every object and passing only the reference to the main object?

                import multiprocessing as mpr
                
                class Element(object):
                    def __init__(self):
                        self.xyz = 33
                
                def fun( ch ):
                    a = ch.recv()
                    print( a[0] )
                    print( a[1].xyz )
                    a[1].xyz = 99
                    print( a[1].xyz )
                
                
                el = Element()
                
                ( pa , ch ) = mpr.Pipe()
                proc = mpr.Process(target=fun , args=( ch, ) )
                
                proc.start()
                pa.send( [ "Hallo" , el ])
                
                print( el.xyz )
                
                proc.join()
                

                The second version dot't produce any error, but the value assigned to a[1].xyz = 99 will be lost in the main process.

                解決方案

                I don't think you understood how the multiprocessing module works.

                When you send something through the pipe, it gets pickled and then unpickled in the subprocess. This means that the subprocess actually has a copy of the original object! That's why the change is "lost". Adding a semaphore wont change anything.

                If you want an object in shared memory you should use multiprocessing.Value, even though this does not handle arbitrary types. Probably multiprocessing.Manager is what you are looking for.

                An other way would be to send a response to the main process providing the modified object.

                這篇關(guān)于Python多處理,傳遞包含信號(hào)量的對(duì)象引用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                How to bind a function to an Action from Qt menubar?(如何將函數(shù)綁定到 Qt 菜單欄中的操作?)
                PyQt progress jumps to 100% after it starts(PyQt 啟動(dòng)后進(jìn)度躍升至 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 刻度標(biāo)簽設(shè)置在固定位置,以便當(dāng)我向左或向右滾動(dòng)時(shí),yaxis 刻度標(biāo)簽應(yīng)該可見
                `QImage` constructor has unknown keyword `data`(`QImage` 構(gòu)造函數(shù)有未知關(guān)鍵字 `data`)
                Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時(shí)顯示進(jìn)度條?)
                    <bdo id='I4ahs'></bdo><ul id='I4ahs'></ul>

                      <tbody id='I4ahs'></tbody>

                      <tfoot id='I4ahs'></tfoot>

                        1. <legend id='I4ahs'><style id='I4ahs'><dir id='I4ahs'><q id='I4ahs'></q></dir></style></legend>

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

                        2. <i id='I4ahs'><tr id='I4ahs'><dt id='I4ahs'><q id='I4ahs'><span id='I4ahs'><b id='I4ahs'><form id='I4ahs'><ins id='I4ahs'></ins><ul id='I4ahs'></ul><sub id='I4ahs'></sub></form><legend id='I4ahs'></legend><bdo id='I4ahs'><pre id='I4ahs'><center id='I4ahs'></center></pre></bdo></b><th id='I4ahs'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='I4ahs'><tfoot id='I4ahs'></tfoot><dl id='I4ahs'><fieldset id='I4ahs'></fieldset></dl></div>
                          主站蜘蛛池模板: 亚洲视频在线观看免费 | 久久综合狠狠综合久久综合88 | 麻豆一区二区三区精品视频 | 九九福利 | 天天综合国产 | 青娱乐一区二区 | 国产在线1 | 亚洲成av片人久久久 | 亚洲欧美精品在线 | 欧美一级片在线 | 99re国产视频 | 国产精品视频www | 国产一级视频在线观看 | 自拍偷拍中文字幕 | 九九热在线视频 | 久久精品国产一区二区三区 | 日韩精品1区2区3区 爱爱综合网 | 午夜欧美 | 亚洲精品女人久久久 | 嫩草懂你的影院入口 | 在线一区 | 狠狠干狠狠操 | 国产高潮好爽受不了了夜色 | 国产精品一区二区免费看 | 黄色小视频入口 | 成人欧美一区二区三区黑人孕妇 | 国产真实精品久久二三区 | 在线国产小视频 | 不卡av电影在线播放 | 亚洲欧美日韩网站 | 夜夜干夜夜操 | 国产成人精品福利 | 91免费观看在线 | 国产人成精品一区二区三 | 操久久| 国产一区2区 | 久久亚洲一区二区三区四区 | 成人精品免费 | av中文字幕在线观看 | 成人综合伊人 | 色视频网站 |