Fix SIGINT when running on multiprocessing

^C was making the script hang (few scenarios possible).
This patch makes sure that the queue operations won't block forever.

Potentially related to #167
This commit is contained in:
Lukasz Dobrzanski 2014-09-06 03:23:55 +01:00 committed by Ian Cordasco
parent e6cb9528d3
commit cbba9faf9e

View file

@ -36,23 +36,38 @@ class BaseQReport(pep8.BaseReport):
# spawn processes # spawn processes
for i in range(self.n_jobs): for i in range(self.n_jobs):
p = multiprocessing.Process(target=self.process_main) p = multiprocessing.Process(target=self.process_main)
p.daemon = True
p.start() p.start()
def stop(self): def stop(self):
# collect queues try:
for i in range(self.n_jobs): # collect queues
self.task_queue.put('DONE') for i in range(self.n_jobs):
self.update_state(self.result_queue.get()) self.task_queue.put('DONE')
super(BaseQReport, self).stop() self.update_state(self.result_queue.get())
except KeyboardInterrupt:
print('... stopped')
finally:
# cleanup queues to unlock threads
while not self.result_queue.empty():
self.result_queue.get_nowait()
while not self.task_queue.empty():
self.task_queue.get_nowait()
super(BaseQReport, self).stop()
def process_main(self): def process_main(self):
if not self._loaded: try:
# Windows needs to parse again the configuration if not self._loaded:
from flake8.main import get_style_guide, DEFAULT_CONFIG # Windows needs to parse again the configuration
get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG) from flake8.main import get_style_guide, DEFAULT_CONFIG
for filename in iter(self.task_queue.get, 'DONE'): get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG)
self.input_file(filename) for filename in iter(self.task_queue.get, 'DONE'):
self.result_queue.put(self.get_state()) self.input_file(filename)
except KeyboardInterrupt:
pass
finally:
self.result_queue.put(self.get_state())
def get_state(self): def get_state(self):
return {'total_errors': self.total_errors, return {'total_errors': self.total_errors,