当子进程已经忽略SIGINT信号时,为什么有必要终止进程


我的英语不好。

我正在阅读ansible 1.1中的代码以下内容取自";ansible-1.1/lib/ansible/runner/__init__.py";

def _executor_hook(job_queue, result_queue):
# attempt workaround of https://github.com/newsapps/beeswithmachineguns/issues/17
# this function also not present in CentOS 6
if HAS_ATFORK:
atfork()
signal.signal(signal.SIGINT, signal.SIG_IGN)
while not job_queue.empty():
try:
host = job_queue.get(block=False)
result_queue.put(multiprocessing_runner._executor(host))
except Queue.Empty:
pass
except:
traceback.print_exc()
class Runner(object):
# ...
def _parallel_exec(self, hosts):
''' handles mulitprocessing when more than 1 fork is required '''
manager = multiprocessing.Manager()
job_queue = manager.Queue()
for host in hosts:
job_queue.put(host)
result_queue = manager.Queue()
workers = []
for i in range(self.forks):
prc = multiprocessing.Process(target=_executor_hook,
args=(job_queue, result_queue))
prc.start()
workers.append(prc)
try:
for worker in workers:
worker.join()
except KeyboardInterrupt:
for worker in workers:
worker.terminate()
worker.join()

当捕捉到错误时,还会调用terminate方法。这和直接传球有什么区别?

try:
for worker in workers:
worker.join()
except KeyboardInterrupt:
pass

这让工人做他们的事情,直到他们完成

try:
for worker in workers:
worker.join()

当你按下ctrl+c 时,它会被调用

except KeyboardInterrupt:
for worker in workers:
worker.terminate()
worker.join()

你基本上是在告诉节目:;不要让工人们完成他们的工作,关闭他们,让我现在就离开这里">

在SIGINT上,它会告诉每个工作者终止,但随后它仍在等待他们全部退出(完成他们在终止时所做的一切(,然后再退出自己。

最新更新