From: Alberto GarcĂ­a Hierro Date: Mon, 13 Sep 2010 18:10:23 +0000 (+0200) Subject: When forking multiple processes, properly initialize the random seed X-Git-Tag: v1.2.0~135^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a352f5ed45e2c8647e87fecd9e9612f85aa45336;p=thirdparty%2Ftornado.git When forking multiple processes, properly initialize the random seed Avoid seeding the random module with the same value in all processes. If available, use the same method used by random.py, otherwise generate a seed using the current time in milliseconds and the pid. --- diff --git a/tornado/httpserver.py b/tornado/httpserver.py index be52c9de9..b85704b44 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -203,6 +203,17 @@ class HTTPServer(object): logging.info("Pre-forking %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) self.io_loop = ioloop.IOLoop.instance() self.io_loop.add_handler( self._socket.fileno(), self._handle_events,