Making the tor launching timeout an arg

Moving the timeout for launching a tor process from being a constant to being
an argument of the function.
This commit is contained in:
Damian Johnson 2011-10-21 07:28:27 -07:00
parent 0a083c8b73
commit 8293c518d0
2 changed files with 19 additions and 12 deletions

View File

@ -8,17 +8,24 @@ import signal
import subprocess
# number of seconds before we time out our attempt to start a tor instance
TOR_INIT_TIMEOUT = 90
DEFAULT_INIT_TIMEOUT = 90
def launch_tor(torrc_path, init_msg_handler = None):
def launch_tor(torrc_path, init_msg_handler = None, timeout = DEFAULT_INIT_TIMEOUT):
"""
Initializes a tor process. This blocks until initialization completes or we
error out.
If tor's data directory is missing or stale then bootstrapping will include
making several requests to the directory authorities which can take a little
while. Usually this is done in 50 seconds or so, but occasionally calls seem
to get stuck, taking well over the default timeout.
Arguments:
torrc_path (str) - location of the torrc for us to use
init_msg_handler (functor) - optional functor that will be provided with
tor's initialization stdout as we get it
timeout (int) - time after which the attempt to start tor is
aborted, no timeouts are applied if None
Returns:
subprocess.Popen instance for the tor subprocess
@ -35,14 +42,14 @@ def launch_tor(torrc_path, init_msg_handler = None):
# starts a tor subprocess, raising an OSError if it fails
tor_process = subprocess.Popen(["tor", "-f", torrc_path], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
# time ourselves out if we reach TOR_INIT_TIMEOUT
def timeout_handler(signum, frame):
# terminates the uninitialized tor process and raise on timeout
tor_process.kill()
raise OSError("reached a %i second timeout without success" % TOR_INIT_TIMEOUT)
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(TOR_INIT_TIMEOUT)
if timeout:
def timeout_handler(signum, frame):
# terminates the uninitialized tor process and raise on timeout
tor_process.kill()
raise OSError("reached a %i second timeout without success" % timeout)
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(timeout)
while True:
init_line = tor_process.stdout.readline().strip()

View File

@ -272,7 +272,7 @@ def call(command, suppress_exc = True):
"""
Issues a command in a subprocess, blocking until completion and returning the
results. This is not actually ran in a shell so pipes and other shell syntax
aren't permitted.
are not permitted.
Arguments:
command (str) - command to be issued
@ -280,7 +280,7 @@ def call(command, suppress_exc = True):
this raises the exception
Returns:
List with the lines of output from the command, None in case of failure if
list with the lines of output from the command, None in case of failure if
suppress_exc is True
Raises: