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

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

    <tfoot id='Tf4GH'></tfoot>

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

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

      在 Python 中,我如何知道一個進程何時完成?

      In Python, how do I know when a process is finished?(在 Python 中,我如何知道一個進程何時完成?)

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

            <small id='9NbIs'></small><noframes id='9NbIs'>

            • <legend id='9NbIs'><style id='9NbIs'><dir id='9NbIs'><q id='9NbIs'></q></dir></style></legend>

              1. <tfoot id='9NbIs'></tfoot>
                本文介紹了在 Python 中,我如何知道一個進程何時完成?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..

                我從 Python GUI (PyGTK) 中啟動一個進程(使用多處理).該過程需要很長時間(約 20 分鐘)才能完成.該過程完成后,我想對其進行清理(提取結果并加入該過程).我如何知道該過程何時完成?

                From within a Python GUI (PyGTK) I start a process (using multiprocessing). The process takes a long time (~20 minutes) to finish. When the process is finished I would like to clean it up (extract the results and join the process). How do I know when the process has finished?

                我的同事建議在父進程中使用一個繁忙的循環(huán)來檢查子進程是否已完成.肯定有更好的方法.

                My colleague suggested a busy loop within the parent process that checks if the child process has finished. Surely there is a better way.

                在 Unix 中,當一個進程被分叉時,子進程完成后從父進程中調用信號處理程序.但我在 Python 中看不到類似的東西.我錯過了什么嗎?

                In Unix, when a process is forked, a signal handler is called from within the parent process when the child process has finished. But I cannot see anything like that in Python. Am I missing something?

                如何從父進程中觀察到子進程的結束?(當然,我不想調用 Process.join(),因為它會凍結 GUI 界面.)

                How is it that the end of a child process can be observed from within the parent process? (Of course, I do not want to call Process.join() as it would freeze up the GUI interface.)

                這個問題不限于多處理:我對多線程也有完全相同的問題.

                This question is not limited to multi-processing: I have exactly the same problem with multi-threading.

                推薦答案

                這個答案真的很簡單!(我只花了 幾天 來解決這個問題.)

                This answer is really simple! (It just took me days to work it out.)

                結合 PyGTK 的 idle_add(),你可以創(chuàng)建一個 AutoJoiningThread.總代碼是微不足道的:

                Combined with PyGTK's idle_add(), you can create an AutoJoiningThread. The total code is borderline trivial:

                class AutoJoiningThread(threading.Thread):
                    def run(self):
                        threading.Thread.run(self)
                        gobject.idle_add(self.join)
                

                如果您想做的不僅僅是加入(例如收集結果),那么您可以擴展上述類以在完成時發(fā)出信號,如下例所示:

                If you want to do more than just join (such as collecting results) then you can extend the above class to emit signals on completion, as is done in the following example:

                import threading
                import time
                import sys
                import gobject
                gobject.threads_init()
                
                class Child:
                    def __init__(self):
                        self.result = None
                
                    def play(self, count):
                        print "Child starting to play."
                        for i in range(count):
                            print "Child playing."
                            time.sleep(1)
                        print "Child finished playing."
                        self.result = 42
                
                    def get_result(self, obj):
                        print "The result was "+str(self.result)
                
                class AutoJoiningThread(threading.Thread, gobject.GObject):
                    __gsignals__ = {
                        'finished': (gobject.SIGNAL_RUN_LAST,
                                     gobject.TYPE_NONE,
                                     ())
                        }
                
                    def __init__(self, *args, **kwargs):
                        threading.Thread.__init__(self, *args, **kwargs)
                        gobject.GObject.__init__(self)
                
                    def run(self):
                        threading.Thread.run(self)
                        gobject.idle_add(self.join)
                        gobject.idle_add(self.emit, 'finished')
                
                    def join(self):
                        threading.Thread.join(self)
                        print "Called Thread.join()"
                
                if __name__ == '__main__':
                    print "Creating child"
                    child = Child()
                    print "Creating thread"
                    thread = AutoJoiningThread(target=child.play,
                                               args=(3,))
                    thread.connect('finished', child.get_result)
                    print "Starting thread"
                    thread.start()
                    print "Running mainloop (Ctrl+C to exit)"
                    mainloop = gobject.MainLoop()
                
                    try:
                        mainloop.run()
                    except KeyboardInterrupt:
                        print "Received KeyboardInterrupt.  Quiting."
                        sys.exit()
                
                    print "God knows how we got here.  Quiting."
                    sys.exit()
                

                上述示例的輸出將取決于線程執(zhí)行的順序,但類似于:

                The output of the above example will depend on the order the threads are executed, but it will be similar to:

                Creating child
                Creating thread
                Starting thread
                Child starting to play.
                 Child playing.
                Running mainloop (Ctrl+C to exit)
                Child playing.
                Child playing.
                Child finished playing.
                Called Thread.join()
                The result was 42
                ^CReceived KeyboardInterrupt.  Quiting.

                不可能以相同的方式創(chuàng)建 AutoJoiningProcess(因為我們不能跨兩個不同的進程調用 idle_add()),但是我們可以使用 AutoJoiningThread 來獲得我們想要的:

                It's not possible to create an AutoJoiningProcess in the same way (because we cannot call idle_add() across two different processes), however we can use an AutoJoiningThread to get what we want:

                class AutoJoiningProcess(multiprocessing.Process):
                    def start(self):
                        thread = AutoJoiningThread(target=self.start_process)
                        thread.start() # automatically joins
                
                    def start_process(self):
                        multiprocessing.Process.start(self)
                        self.join()
                

                為了演示 AutoJoiningProcess 這里是另一個例子:

                To demonstrate AutoJoiningProcess here is another example:

                import threading
                import multiprocessing
                import time
                import sys
                import gobject
                gobject.threads_init()
                
                class Child:
                    def __init__(self):
                        self.result = multiprocessing.Manager().list()
                
                    def play(self, count):
                        print "Child starting to play."
                        for i in range(count):
                            print "Child playing."
                            time.sleep(1)
                    print "Child finished playing."
                        self.result.append(42)
                
                    def get_result(self, obj):
                        print "The result was "+str(self.result)
                
                class AutoJoiningThread(threading.Thread, gobject.GObject):
                    __gsignals__ = {
                        'finished': (gobject.SIGNAL_RUN_LAST,
                                     gobject.TYPE_NONE,
                                     ())
                    }
                
                    def __init__(self, *args, **kwargs):
                        threading.Thread.__init__(self, *args, **kwargs)
                        gobject.GObject.__init__(self)
                
                    def run(self):
                        threading.Thread.run(self)
                        gobject.idle_add(self.join)
                        gobject.idle_add(self.emit, 'finished')
                
                    def join(self):
                        threading.Thread.join(self)
                        print "Called Thread.join()"
                
                class AutoJoiningProcess(multiprocessing.Process, gobject.GObject):
                    __gsignals__ = {
                        'finished': (gobject.SIGNAL_RUN_LAST,
                                     gobject.TYPE_NONE,
                                     ())
                        }
                
                    def __init__(self, *args, **kwargs):
                        multiprocessing.Process.__init__(self, *args, **kwargs)
                        gobject.GObject.__init__(self)
                
                    def start(self):
                        thread = AutoJoiningThread(target=self.start_process)
                        thread.start()
                
                    def start_process(self):
                        multiprocessing.Process.start(self)
                        self.join()
                        gobject.idle_add(self.emit, 'finished')
                
                    def join(self):
                        multiprocessing.Process.join(self)
                        print "Called Process.join()"
                
                if __name__ == '__main__':
                    print "Creating child"
                    child = Child()
                    print "Creating thread"
                    process = AutoJoiningProcess(target=child.play,
                                               args=(3,))
                    process.connect('finished',child.get_result)
                    print "Starting thread"
                    process.start()
                    print "Running mainloop (Ctrl+C to exit)"
                    mainloop = gobject.MainLoop()
                
                    try:
                        mainloop.run()
                    except KeyboardInterrupt:
                        print "Received KeyboardInterrupt.  Quiting."
                        sys.exit()
                
                    print "God knows how we got here.  Quiting."
                    sys.exit()
                

                生成的輸出將與上面的示例非常相似,只是這次我們同時加入了進程和伴隨線程:

                The resulting output will be very similar to the example above, except this time we have both the process joining and it's attendant thread joining too:

                Creating child
                Creating thread
                Starting thread
                Running mainloop (Ctrl+C to exit)
                 Child starting to play.
                Child playing.
                Child playing.
                Child playing.
                Child finished playing.
                Called Process.join()
                The result was [42]
                Called Thread.join()
                ^CReceived KeyboardInterrupt.  Quiting.

                不幸的是:

                1. 由于使用了 idle_add(),此解決方案依賴于 gobject.PyGTK 使用 gobject.
                2. 這不是真正的父子關系.如果其中一個線程是由另一個線程啟動的,那么它仍然會被運行主循環(huán)的線程加入,而不是父線程.這個問題也適用于 AutoJoiningProcess,除了我想會拋出異常.

                因此要使用這種方法,最好只從主循環(huán)/GUI 中創(chuàng)建線程/進程.

                Thus to use this approach, it would be best to only create threads/process from within the mainloop/GUI.

                這篇關于在 Python 中,我如何知道一個進程何時完成?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                相關文檔推薦

                What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數(shù)傳遞給 pool.map() 函數(shù))
                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() 管理的函數(shù)?)
                yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)
              2. <tfoot id='yVzxW'></tfoot>
                <i id='yVzxW'><tr id='yVzxW'><dt id='yVzxW'><q id='yVzxW'><span id='yVzxW'><b id='yVzxW'><form id='yVzxW'><ins id='yVzxW'></ins><ul id='yVzxW'></ul><sub id='yVzxW'></sub></form><legend id='yVzxW'></legend><bdo id='yVzxW'><pre id='yVzxW'><center id='yVzxW'></center></pre></bdo></b><th id='yVzxW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='yVzxW'><tfoot id='yVzxW'></tfoot><dl id='yVzxW'><fieldset id='yVzxW'></fieldset></dl></div>

                  <bdo id='yVzxW'></bdo><ul id='yVzxW'></ul>
                • <legend id='yVzxW'><style id='yVzxW'><dir id='yVzxW'><q id='yVzxW'></q></dir></style></legend>
                    1. <small id='yVzxW'></small><noframes id='yVzxW'>

                          <tbody id='yVzxW'></tbody>
                          主站蜘蛛池模板: 国产成人精品一区二区三区视频 | 久久精品二区 | 黄色片免费看视频 | 五月激情婷婷六月 | 嫩草网| 免费久久精品 | 国产精品久久久久无码av | 日韩第一页 | 亚洲香蕉在线视频 | 久久黄网| 一级h片 | 国产二区三区 | 欧美在线视频一区 | 国产激情视频在线 | 国产精品久久久久久久岛一牛影视 | 四虎最新 | 日韩中文字幕一区二区 | 久久草在线视频 | 久草.com| 国产精品欧美一区二区三区 | 欧美久久久网站 | 91精品国产综合久久久久久蜜臀 | 欧美一区二区三区视频在线观看 | 欧美国产精品一区二区 | 久久高清精品 | 视频国产一区 | 性色的免费视频 | 久久久精彩视频 | 99re视频在线| 农村妇女毛片精品久久久 | 先锋资源亚洲 | 男女啪啪高潮无遮挡免费动态 | 91视频三区 | 国产精品呻吟久久av凹凸 | 久久午夜精品 | 黄色一级大片在线免费看产 | 99久久99热这里只有精品 | 国产精品178页 | 狠狠狠干| 91久久久久久久久久久久久 | 国产日韩欧美一区 |