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,22 +36,37 @@ 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):
try:
# collect queues # collect queues
for i in range(self.n_jobs): for i in range(self.n_jobs):
self.task_queue.put('DONE') self.task_queue.put('DONE')
self.update_state(self.result_queue.get()) 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() super(BaseQReport, self).stop()
def process_main(self): def process_main(self):
try:
if not self._loaded: if not self._loaded:
# Windows needs to parse again the configuration # Windows needs to parse again the configuration
from flake8.main import get_style_guide, DEFAULT_CONFIG from flake8.main import get_style_guide, DEFAULT_CONFIG
get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG) get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG)
for filename in iter(self.task_queue.get, 'DONE'): for filename in iter(self.task_queue.get, 'DONE'):
self.input_file(filename) self.input_file(filename)
except KeyboardInterrupt:
pass
finally:
self.result_queue.put(self.get_state()) self.result_queue.put(self.get_state())
def get_state(self): def get_state(self):