From ea40e64b9c310e4f7c081065c877ccceb69b877e Mon Sep 17 00:00:00 2001 From: j Date: Sun, 24 Jan 2016 18:01:42 +0530 Subject: [PATCH] restart tor daemon if its no longer running --- oml/tor.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/oml/tor.py b/oml/tor.py index cc9b81e..3af2e3c 100644 --- a/oml/tor.py +++ b/oml/tor.py @@ -2,6 +2,7 @@ import os import re import sys import subprocess +import time from threading import Thread import distutils @@ -20,6 +21,7 @@ logger = logging.getLogger(__name__) class TorDaemon(Thread): installing = False + running = True def __init__(self): self._status = [] @@ -69,14 +71,17 @@ DirReqStatistics 0 tor = get_tor() if tor: cmd = [tor, '--defaults-torrc', defaults, '-f', torrc] - self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1, - universal_newlines=True, close_fds=True, start_new_session=True) - for line in self.p.stdout: - self._status.append(line) - logger.debug(line) + while self.running: + self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1, + universal_newlines=True, close_fds=True, start_new_session=True) + for line in self.p.stdout: + self._status.append(line) + logger.debug(line) + time.sleep(0.5) self.p = None - def shutdown(self): + def kill(self): + self.running = False if self.p: self.p.kill() @@ -154,7 +159,7 @@ class Tor(object): #self.controller.remove_event_listener(self.connection_change) self.controller.close() if self.daemon: - self.daemon.shutdown() + self.daemon.kill() except: logger.debug('shutdown exception', exc_info=True) pass