問題描述
如何在多進程 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.
推薦答案
先前接受的解決方案 有競爭條件,但它沒有使用 map
和 async
函數.
The previously accepted solution has race conditions and it does not work with map
and async
functions.
multiprocessing.Pool
處理Ctrl+C/SIGINT
的正確方法是:
- 在創建進程
Pool
之前讓進程忽略SIGINT
.這種方式創建的子進程繼承SIGINT
處理程序. - 在創建
Pool
后,在父進程中恢復原始SIGINT
處理程序. - 使用
map_async
和apply_async
而不是阻塞map
和apply
. - 使用超時等待結果,因為默認阻塞等待忽略所有信號.這是 Python 錯誤 https://bugs.python.org/issue8296.
- Make the process ignore
SIGINT
before a processPool
is created. This way created child processes inheritSIGINT
handler. - Restore the original
SIGINT
handler in the parent process after aPool
has been created. - Use
map_async
andapply_async
instead of blockingmap
andapply
. - 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模板網!