My English is not good.
I’m reading the code from ansible 1.1
The following is taken from "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()
When an error is caught, the terminate method is also called.
What is the difference between this and a direct pass?
try:
for worker in workers:
worker.join()
except KeyboardInterrupt:
pass
2
Answers
This lets the workers do their thing until they’re done
This gets called when you press ctrl+c
You are basically telling the program: "Don’t let the workers finish their stuff, shut them down and get me out of here NOW"
On SIGINT, it will tell each worker to terminate, but then it still waits for them all to exit (completing whatever they do on termination) before it exits itself.