From e6cfdf6a1f3d5d299995f78501d44630d300d379 Mon Sep 17 00:00:00 2001 From: Ilan Steemers Date: Thu, 18 Jun 2015 17:55:52 +0200 Subject: [PATCH] Better exit strategy: -Pusher is stopped -Poison pills are put on the task queue -Wait for workers to clear the queue and exit -Poison pill is put in the result queue -Main process exits when monitor has finished processing results --- django_q/main.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/django_q/main.py b/django_q/main.py index 7506c19..5afcca0 100644 --- a/django_q/main.py +++ b/django_q/main.py @@ -108,11 +108,17 @@ class Cluster(object): logger.info('{} stopping pool processes'.format(name)) # Stopping pusher self.event_stop.set() - # Stopping monitor - self.done_queue.put('STOP') - # Stopping workers + # Putting poison pills in the queue for _ in self.pool: self.task_queue.put('STOP') + while len(self.pool) > 2: + for p in list(self.pool): + if not p.is_alive(): + logger.debug('{} stopped gracefully'.format(p.pid)) + self.pool.remove(p) + sleep(0.2) + # Finally stop the monitor + self.done_queue.put('STOP') def sig_handler(self, signum, frame): logger.debug('{} got signal {}'.format(current_process().name, SIGNAL_NAMES.get(signum, 'UNKNOWN')))