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

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

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

      <bdo id='UF9pS'></bdo><ul id='UF9pS'></ul>
    <legend id='UF9pS'><style id='UF9pS'><dir id='UF9pS'><q id='UF9pS'></q></dir></style></legend>

    1. <tfoot id='UF9pS'></tfoot>
    2. 如何使用 multiprocessing.Queue.get 方法?

      How to use multiprocessing.Queue.get method?(如何使用 multiprocessing.Queue.get 方法?)
      <i id='nVTsc'><tr id='nVTsc'><dt id='nVTsc'><q id='nVTsc'><span id='nVTsc'><b id='nVTsc'><form id='nVTsc'><ins id='nVTsc'></ins><ul id='nVTsc'></ul><sub id='nVTsc'></sub></form><legend id='nVTsc'></legend><bdo id='nVTsc'><pre id='nVTsc'><center id='nVTsc'></center></pre></bdo></b><th id='nVTsc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='nVTsc'><tfoot id='nVTsc'></tfoot><dl id='nVTsc'><fieldset id='nVTsc'></fieldset></dl></div>
        <tbody id='nVTsc'></tbody>

      <legend id='nVTsc'><style id='nVTsc'><dir id='nVTsc'><q id='nVTsc'></q></dir></style></legend>
      • <bdo id='nVTsc'></bdo><ul id='nVTsc'></ul>

            <tfoot id='nVTsc'></tfoot>
          1. <small id='nVTsc'></small><noframes id='nVTsc'>

              • 本文介紹了如何使用 multiprocessing.Queue.get 方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                限時送ChatGPT賬號..

                下面的代碼將三個數(shù)字放在一個隊列中.然后它嘗試從隊列中取回號碼.但它永遠不會.如何從隊列中獲取數(shù)據(jù)?

                The code below places three numbers in a queue. Then it attempts to get the numbers back from the queue. But it never does. How to get the data from the queue?

                import multiprocessing
                
                queue = multiprocessing.Queue()
                
                for i in range(3):
                    queue.put(i)
                
                while not queue.empty():
                    print queue.get()
                

                推薦答案

                我最初在閱讀@Martijn Pieters 后刪除了這個答案,因為他更詳細(xì)和更早地描述了為什么這不起作用".然后我意識到,OP 示例中的用例不太適合

                I originally deleted this answer after I read @Martijn Pieters', since he decribed the "why this doesn't work" in more detail and earlier. Then I realized, that the use case in OP's example doesn't quite fit to the canonical sounding title of

                如何使用 multiprocessing.Queue.get 方法".

                "How to use multiprocessing.Queue.get method".

                那不是因為有演示中不涉及子進程,但是因為在實際應(yīng)用程序中,幾乎沒有一個隊列是預(yù)先填充的,并且只在之后讀取,但是讀取并且寫入發(fā)生在中間的等待時間之間.Martijn 展示的擴展演示代碼在通常情況下不起作用,因為當(dāng)排隊跟不上讀取速度時,while 循環(huán)會過早中斷.所以這里是重新加載的答案,它能夠處理通常的交錯提要和讀取場景:

                That's not because there's no child process involved for demonstration, but because in real applications hardly ever a queue is pre-filled and only read out after, but reading and writing happens interleaved with waiting times in between. The extended demonstration code Martijn showed, wouldn't work in the usual scenarios, because the while loop would break too soon when enqueuing doesn't keep up with reading. So here is the answer reloaded, which is able to deal with the usual interleaved feeds & reads scenarios:

                不要依賴 queue.empty 檢查同步.

                Don't rely on queue.empty checks for synchronization.

                在將對象放入空隊列后,隊列的 empty() 方法返回 False 并且 get_nowait() 可以在不引發(fā) queue.Empty 的情況下返回之前,可能會有一個無限小的延遲....

                After putting an object on an empty queue there may be an infinitesimal delay before the queue’s empty() method returns False and get_nowait() can return without raising queue.Empty. ...

                empty()

                如果隊列為空,則返回 True,否則返回 False.由于多線程/多處理語義,這是不可靠的.docs

                Return True if the queue is empty, False otherwise. Because of multithreading/multiprocessing semantics, this is not reliable. docs

                從隊列中使用 for msg in iter(queue.get, sentinel):.get(),您可以通過傳遞一個哨兵值...iter(callable, sentinel)?

                Either use for msg in iter(queue.get, sentinel): to .get() from the queue, where you break out of the loop by passing a sentinel value...iter(callable, sentinel)?

                from multiprocessing import Queue
                
                SENTINEL = None
                
                if __name__ == '__main__':
                
                    queue = Queue()
                
                    for i in [*range(3), SENTINEL]:
                        queue.put(i)
                
                    for msg in iter(queue.get, SENTINEL):
                        print(msg)
                

                ...如果您需要非阻塞解決方案,請使用 get_nowait() 并處理可能的 queue.Empty 異常.

                ...or use get_nowait() and handle a possible queue.Empty exception if you need a non-blocking solution.

                from multiprocessing import Queue
                from queue import Empty
                import time
                
                SENTINEL = None
                
                if __name__ == '__main__':
                
                    queue = Queue()
                
                    for i in [*range(3), SENTINEL]:
                        queue.put(i)
                
                    while True:
                        try:
                            msg = queue.get_nowait()
                            if msg == SENTINEL:
                                break
                            print(msg)
                        except Empty:
                            # do other stuff
                            time.sleep(0.1)
                

                如果只有一個進程和該進程中的一個線程正在讀取隊列,也可以將最后一個代碼片段交換為:

                In case only one process and only one thread within this process is reading the queue, it would be also possible to exchange the last code snippet with:

                while True:
                    if not queue.empty():  # this is not an atomic operation ...
                        msg = queue.get()  # ... thread could be interrupted in between
                        if msg == SENTINEL:
                            break
                        print(msg)
                    else:
                        # do other stuff
                        time.sleep(0.1)
                

                由于線程可能會在檢查 if not queue 之間刪除 GIL.empty()queue.get(),這不適用于進程中的多線程隊列讀取.如果多個進程正在從隊列中讀取,這同樣適用.

                Since a thread could drop the GIL in between checking if not queue.empty() and queue.get(), this wouldn't be suitable for multi-threaded queue-reads in a process. The same applies if multiple processes are reading from the queue.

                對于單一生產(chǎn)者/單一消費者的場景,使用 multiprocessing.Pipe 而不是 multiprocessing.Queue 就足夠了,而且性能更高.

                For single-producer / single-consumer scenarios, using a multiprocessing.Pipe instead of multiprocessing.Queue would be sufficient and more performant, though.

                這篇關(guān)于如何使用 multiprocessing.Queue.get 方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guā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 多進程池.當(dāng)其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - 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)
                      <tbody id='1rNEj'></tbody>

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

                        • <bdo id='1rNEj'></bdo><ul id='1rNEj'></ul>

                          <small id='1rNEj'></small><noframes id='1rNEj'>

                          <legend id='1rNEj'><style id='1rNEj'><dir id='1rNEj'><q id='1rNEj'></q></dir></style></legend>
                        • <tfoot id='1rNEj'></tfoot>
                        • 主站蜘蛛池模板: 国产午夜精品久久久久免费视高清 | 最新国产精品精品视频 | 99精品欧美一区二区三区综合在线 | 欧美激情网站 | 国产探花在线观看视频 | 欧美日韩福利视频 | 欧美自拍视频 | 亚洲精品乱码久久久久久久久 | 国产精品久久久久久久久久久免费看 | 性大毛片视频 | 日韩不卡视频在线 | 国产乱肥老妇国产一区二 | 国产在线精品一区二区 | 国产成人精品网站 | 台湾av在线 | 精品视频www| 日韩国产一区二区三区 | 特级黄一级播放 | 亚洲人在线播放 | 99视频 | 欧美99 | 国产精品日韩欧美一区二区三区 | 国产精品网址 | 久久久.com| 国产精品久久久久久久免费观看 | 免费日韩网站 | 成人av一区 | 日本欧美国产 | 麻豆av电影网 | 日韩一区二区在线视频 | 在线永久看片免费的视频 | 色婷婷亚洲一区二区三区 | 久久久成人一区二区免费影院 | 国产日韩一区二区 | 久热精品在线 | 天天操精品视频 | av一级毛片| 9999视频 | 国产乱码久久久久久一区二区 | 亚洲电影免费 | 国产91一区|