From: Ben Darnell Date: Tue, 3 Aug 2010 21:21:24 +0000 (-0700) Subject: Use the more portable multiprocessing.cpu_count() to detect the number X-Git-Tag: v1.1.0~54 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9a083095ffa709fc0531a7deb2d16e149b4ebd37;p=thirdparty%2Ftornado.git Use the more portable multiprocessing.cpu_count() to detect the number of cpus where available (python 2.6). --- diff --git a/tornado/httpserver.py b/tornado/httpserver.py index 8c4cee9dd..6cece5d7a 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -41,6 +41,26 @@ try: except ImportError: ssl = None +try: + import multiprocessing # Python 2.6+ +except ImportError: + multiprocessing = None + +def _cpu_count(): + if multiprocessing is not None: + try: + return multiprocessing.cpu_count() + except NotImplementedError: + pass + try: + return os.sysconf("SC_NPROCESSORS_CONF") + except ValueError: + pass + logging.error("Could not detect number of processors; " + "running with one process") + return 1 + + class HTTPServer(object): """A non-blocking, single-threaded HTTP server. @@ -168,13 +188,7 @@ class HTTPServer(object): assert not self._started self._started = True if num_processes is None or num_processes <= 0: - # Use sysconf to detect the number of CPUs (cores) - try: - num_processes = os.sysconf("SC_NPROCESSORS_CONF") - except ValueError: - logging.error("Could not get num processors from sysconf; " - "running with one process") - num_processes = 1 + num_processes = _cpu_count() if num_processes > 1 and ioloop.IOLoop.initialized(): logging.error("Cannot run in multiple processes: IOLoop instance " "has already been initialized. You cannot call "