From: Ben Darnell Date: Tue, 5 Jul 2011 00:32:31 +0000 (-0700) Subject: Refactor random-seed code out of fork_processes X-Git-Tag: v2.1.0~121 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=70bcb6e3ef9ed811eddeacace6bdd36b6974f483;p=thirdparty%2Ftornado.git Refactor random-seed code out of fork_processes --- diff --git a/tornado/process.py b/tornado/process.py index 86583293c..0bd4bde7c 100644 --- a/tornado/process.py +++ b/tornado/process.py @@ -18,8 +18,11 @@ import logging import os +import sys import time +from binascii import hexlify + from tornado import ioloop try: @@ -41,6 +44,20 @@ def cpu_count(): logging.error("Could not detect number of processors; assuming 1") return 1 +def _reseed_random(): + if 'random' not in sys.modules: + return + import random + # If os.urandom is available, this method does the same thing as + # random.seed (at least as of python 2.6). If os.urandom is not + # available, we mix in the pid in addition to a timestamp. + try: + seed = long(hexlify(os.urandom(16)), 16) + except NotImplementedError: + seed(int(time.time() * 1000) ^ os.getpid()) + random.seed(seed) + + _processes_forked = False def fork_processes(num_processes): @@ -71,16 +88,6 @@ def fork_processes(num_processes): logging.info("Starting %d server processes", num_processes) for i in range(num_processes): if os.fork() == 0: - import random - from binascii import hexlify - try: - # If available, use the same method as - # random.py - seed = long(hexlify(os.urandom(16)), 16) - except NotImplementedError: - # Include the pid to avoid initializing two - # processes to the same value - seed(int(time.time() * 1000) ^ os.getpid()) - random.seed(seed) + _reseed_random() return os.waitpid(-1, 0)