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

  1. <small id='8iCwm'></small><noframes id='8iCwm'>

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

      • <bdo id='8iCwm'></bdo><ul id='8iCwm'></ul>
    1. <tfoot id='8iCwm'></tfoot>
    2. <legend id='8iCwm'><style id='8iCwm'><dir id='8iCwm'><q id='8iCwm'></q></dir></style></legend>

    3. python pool apply_async 和 map_async 不會阻塞完整隊列

      python pool apply_async and map_async do not block on full queue(python pool apply_async 和 map_async 不會阻塞完整隊列)
      <legend id='mvsh5'><style id='mvsh5'><dir id='mvsh5'><q id='mvsh5'></q></dir></style></legend>
        <tbody id='mvsh5'></tbody>

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

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

            • <tfoot id='mvsh5'></tfoot>

                本文介紹了python pool apply_async 和 map_async 不會阻塞完整隊列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..

                我對 python 還很陌生.我正在使用多處理模塊讀取標準輸入上的文本行,以某種方式轉換它們并將它們寫入數據庫.這是我的代碼片段:

                I am fairly new to python. I am using the multiprocessing module for reading lines of text on stdin, converting them in some way and writing them into a database. Here's a snippet of my code:

                batch = []
                pool = multiprocessing.Pool(20)
                i = 0
                for i, content in enumerate(sys.stdin):
                    batch.append(content)
                    if len(batch) >= 10000:
                        pool.apply_async(insert, args=(batch,i+1))
                        batch = []
                pool.apply_async(insert, args=(batch,i))
                pool.close()
                pool.join()
                

                現在一切正常,直到我開始處理巨大的輸入文件(數億行),然后通過管道傳輸到我的 python 程序中.在某些時候,當我的數據庫變慢時,我會看到內存已滿.

                Now that all works fine, until I get to process huge input files (hundreds of millions of lines) that i pipe into my python program. At some point, when my database gets slower, I see the memory getting full.

                玩了一會兒,發現 pool.apply_async 和 pool.map_async 從來沒有阻塞過,所以要處理的調用隊列越來越大.

                After some playing, it turned out that pool.apply_async as well as pool.map_async never ever block, so that the queue of the calls to be processed grows bigger and bigger.

                解決我的問題的正確方法是什么?我希望我可以設置一個參數,一旦達到某個隊列長度,它將阻止 pool.apply_async 調用.Java 中的 AFAIR 可以為此目的為 ThreadPoolExecutor 提供一個具有固定長度的 BlockingQueue.

                What is the correct approach to my problem? I would expect a parameter that I can set, that will block the pool.apply_async call, as soon as a certain queue length has been reached. AFAIR in Java one can give the ThreadPoolExecutor a BlockingQueue with a fixed length for that purpose.

                謝謝!

                推薦答案

                apply_asyncmap_async 函數旨在不阻塞主進程.為了做到這一點,Pool 維護了一個內部 Queue,遺憾的是它的大小無法更改.

                The apply_async and map_async functions are designed not to block the main process. In order to do so, the Pool maintains an internal Queue which size is unfortunately impossible to change.

                解決問題的方法是使用 Semaphore 以您希望隊列的大小進行初始化.在為池提供數據之前以及在工作人員完成任務之后獲取和釋放信號量.

                The way the problem can be solved is by using a Semaphore initialized with the size you want the queue to be. You acquire and release the semaphore before feeding the pool and after a worker has completed the task.

                這是一個使用 Python 2.6 或更高版本的示例.

                Here's an example working with Python 2.6 or greater.

                from threading import Semaphore
                from multiprocessing import Pool
                
                def task_wrapper(f):
                    """Python2 does not allow a callback for method raising exceptions,
                    this wrapper ensures the code run into the worker will be exception free.
                
                    """
                    try:
                        return f()
                    except:
                        return None
                
                class TaskManager(object):
                    def __init__(self, processes, queue_size):
                        self.pool = Pool(processes=processes)
                        self.workers = Semaphore(processes + queue_size)
                
                    def new_task(self, f):
                        """Start a new task, blocks if queue is full."""
                        self.workers.acquire()
                        self.pool.apply_async(task_wrapper, args=(f, ), callback=self.task_done))
                
                    def task_done(self):
                        """Called once task is done, releases the queue is blocked."""
                        self.workers.release()
                

                另一個使用 concurrent.futures 池實現的示例.

                Another example using concurrent.futures pools implementation.

                這篇關于python pool apply_async 和 map_async 不會阻塞完整隊列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
                <i id='EjvhI'><tr id='EjvhI'><dt id='EjvhI'><q id='EjvhI'><span id='EjvhI'><b id='EjvhI'><form id='EjvhI'><ins id='EjvhI'></ins><ul id='EjvhI'></ul><sub id='EjvhI'></sub></form><legend id='EjvhI'></legend><bdo id='EjvhI'><pre id='EjvhI'><center id='EjvhI'></center></pre></bdo></b><th id='EjvhI'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='EjvhI'><tfoot id='EjvhI'></tfoot><dl id='EjvhI'><fieldset id='EjvhI'></fieldset></dl></div>

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

                      <bdo id='EjvhI'></bdo><ul id='EjvhI'></ul>
                        <tbody id='EjvhI'></tbody>
                    • <tfoot id='EjvhI'></tfoot>
                      <legend id='EjvhI'><style id='EjvhI'><dir id='EjvhI'><q id='EjvhI'></q></dir></style></legend>

                          主站蜘蛛池模板: 免费艹逼视频 | 在线免费观看黄色av | 亚洲瑟瑟 | 国产精品久久久久一区二区三区 | 一级毛片在线看 | а_天堂中文最新版地址 | 天天想天天干 | 黄网在线观看 | 福利片在线观看 | 国产精品99久久久久久久vr | 91国内精品久久 | 性视频网 | 国产精品亚洲精品 | 欧美激情综合五月色丁香小说 | 综合久久综合久久 | 日韩一区二区三区在线观看视频 | 亚洲国产看片 | 成人做爰www免费看视频网站 | 一级做a爰片久久毛片免费看 | 天天射影院| 一区二区三区高清在线观看 | 日韩一区二区三区视频 | 男人亚洲天堂 | 古典武侠第一页久久777 | 伊人激情综合网 | 久久精品小视频 | 欧美成人精品在线 | 亚洲天堂网站 | 91免费在线播放 | 成人精品在线观看 | 欧美精品一区三区 | 一区二区三区亚洲视频 | 婷婷精品| 韩国久久 | 在线观看国产三级 | 日韩欧美精品在线播放 | 免费久久精品视频 | 久久免费精品 | 乱码av午夜噜噜噜噜动漫 | 日日操日日舔 | 欧美二区在线 |