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

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

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

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

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

        捕捉 Ctrl+C/SIGINT 并在 python 中優雅地退出多進程

        Catch Ctrl+C / SIGINT and exit multiprocesses gracefully in python(捕捉 Ctrl+C/SIGINT 并在 python 中優雅地退出多進程)
      1. <legend id='F6Gqm'><style id='F6Gqm'><dir id='F6Gqm'><q id='F6Gqm'></q></dir></style></legend>
          <bdo id='F6Gqm'></bdo><ul id='F6Gqm'></ul>

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

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

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

                  本文介紹了捕捉 Ctrl+C/SIGINT 并在 python 中優雅地退出多進程的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  如何在多進程 python 程序中捕獲 Ctrl+C 并優雅地退出所有進程,我需要該解決方案在 unix 和 windows 上都可以工作.我嘗試了以下方法:

                  How do I catch a Ctrl+C in multiprocess python program and exit all processes gracefully, I need the solution to work both on unix and windows. I've tried the following:

                  import multiprocessing
                  import time
                  import signal
                  import sys
                  
                  jobs = []
                  
                  def worker():
                      signal.signal(signal.SIGINT, signal_handler)
                      while(True):
                          time.sleep(1.1234)
                          print "Working..."
                  
                  def signal_handler(signal, frame):
                      print 'You pressed Ctrl+C!'
                      # for p in jobs:
                      #     p.terminate()
                      sys.exit(0)
                  
                  if __name__ == "__main__":
                      for i in range(50):
                          p = multiprocessing.Process(target=worker)
                          jobs.append(p)
                          p.start()
                  

                  它有點工作,但我認為這不是正確的解決方案.

                  And it's kind of working, but I don't think it's the right solution.

                  推薦答案

                  先前接受的解決方案 有競爭條件,但它沒有使用 mapasync 函數.

                  The previously accepted solution has race conditions and it does not work with map and async functions.

                  multiprocessing.Pool處理Ctrl+C/SIGINT的正確方法是:

                  1. 在創建進程 Pool 之前讓進程忽略 SIGINT.這種方式創建的子進程繼承 SIGINT 處理程序.
                  2. 在創建 Pool 后,在父進程中恢復原始 SIGINT 處理程序.
                  3. 使用 map_asyncapply_async 而不是阻塞 mapapply.
                  4. 使用超時等待結果,因為默認阻塞等待忽略所有信號.這是 Python 錯誤 https://bugs.python.org/issue8296.
                  1. Make the process ignore SIGINT before a process Pool is created. This way created child processes inherit SIGINT handler.
                  2. Restore the original SIGINT handler in the parent process after a Pool has been created.
                  3. Use map_async and apply_async instead of blocking map and apply.
                  4. Wait on the results with timeout because the default blocking waits to ignore all signals. This is Python bug https://bugs.python.org/issue8296.

                  <小時>

                  把它放在一起:


                  Putting it together:

                  #!/bin/env python
                  from __future__ import print_function
                  
                  import multiprocessing
                  import os
                  import signal
                  import time
                  
                  def run_worker(delay):
                      print("In a worker process", os.getpid())
                      time.sleep(delay)
                  
                  def main():
                      print("Initializng 2 workers")
                      original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
                      pool = multiprocessing.Pool(2)
                      signal.signal(signal.SIGINT, original_sigint_handler)
                      try:
                          print("Starting 2 jobs of 5 seconds each")
                          res = pool.map_async(run_worker, [5, 5])
                          print("Waiting for results")
                          res.get(60) # Without the timeout this blocking call ignores all signals.
                      except KeyboardInterrupt:
                          print("Caught KeyboardInterrupt, terminating workers")
                          pool.terminate()
                      else:
                          print("Normal termination")
                          pool.close()
                      pool.join()
                  
                  if __name__ == "__main__":
                      main()
                  

                  正如@YakovShklarov 所指出的,在父進程中忽略信號和取消忽略信號之間有一個時間窗口,在此期間信號可能會丟失.使用 pthread_sigmask 代替在父進程中臨時阻止信號的傳遞可以防止信號丟失,但是在 Python-2 中不可用.

                  As @YakovShklarov noted, there is a window of time between ignoring the signal and unignoring it in the parent process, during which the signal can be lost. Using pthread_sigmask instead to temporarily block the delivery of the signal in the parent process would prevent the signal from being lost, however, it is not available in Python-2.

                  這篇關于捕捉 Ctrl+C/SIGINT 并在 python 中優雅地退出多進程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
                  • <small id='Y7aid'></small><noframes id='Y7aid'>

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

                      1. <tfoot id='Y7aid'></tfoot>

                          <tbody id='Y7aid'></tbody>
                            <bdo id='Y7aid'></bdo><ul id='Y7aid'></ul>
                            <i id='Y7aid'><tr id='Y7aid'><dt id='Y7aid'><q id='Y7aid'><span id='Y7aid'><b id='Y7aid'><form id='Y7aid'><ins id='Y7aid'></ins><ul id='Y7aid'></ul><sub id='Y7aid'></sub></form><legend id='Y7aid'></legend><bdo id='Y7aid'><pre id='Y7aid'><center id='Y7aid'></center></pre></bdo></b><th id='Y7aid'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Y7aid'><tfoot id='Y7aid'></tfoot><dl id='Y7aid'><fieldset id='Y7aid'></fieldset></dl></div>
                          • 主站蜘蛛池模板: 中文字幕精品视频 | 欧美一区二区三区在线观看视频 | 小视频你懂得 | 人人做人人澡人人爽欧美 | 国产一区h | 精品久久久久久亚洲综合网 | 男人天堂网址 | 亚洲看片网站 | 日本三级电影在线观看视频 | 欧美综合国产精品久久丁香 | 欧美精品久久久久 | 成人黄色电影在线观看 | 97caoporn国产免费人人 | 久久精品一区二 | 亚洲成av人影片在线观看 | 亚洲免费一区二区 | 国内精品视频在线观看 | 99re视频在线 | 欧美在线国产精品 | 2018国产大陆天天弄 | 国产目拍亚洲精品99久久精品 | 羞羞视频在线观看网站 | 国产激情综合五月久久 | 亚洲乱码一区二区三区在线观看 | 毛片一区二区三区 | 欧美激情精品久久久久久变态 | 99久久久久久99国产精品免 | 亚洲综合大片69999 | 久久久无码精品亚洲日韩按摩 | 91久久视频 | 国产蜜臀97一区二区三区 | 精品久久国产 | 91天堂网| 久久久久国产精品一区二区 | 在线免费黄色小视频 | 亚洲国产黄色av | 在线三级电影 | 国产精品一区二区久久 | 欧美日韩一区在线 | 久久一级 | 久在线视频播放免费视频 |