Commits

DasIch  committed 49f9eb1 Merge

Automated merge with ssh://bitbucket.org/birkenfeld/sphinx

  • Participants
  • Parent commits 7c00c4c, 9bf4193

Comments (0)

Files changed (2)

File sphinx/util/multiprocessingimpl.py

+# -*- coding: utf-8 -*-
+"""
+    sphinx.util.multiprocessingimpl
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+    This module provides the multiprocessing implementation used by Sphinx. You
+    should be aware of the fact that although being similar in API to
+    :mod:`multiprocessing` the implementation may not be based on it.
+
+    :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+from itertools import imap
+
+GreenPool = MultiProcessingPool = None
+try:
+    from eventlet import GreenPool
+except ImportError:
+    try:
+        from multiprocessing import Pool as MultiProcessingPool
+    except ImportError:
+        pass
+
+try:
+    from multiprocessing import cpu_count as _cpu_count
+except ImportError:
+    def _cpu_count():
+        raise NotImplementedError()
+
+
+# try to guess the number of cpus on this system, default to 2 as most modern
+# systems should have 2 by now
+CPU_COUNT_SYSCONF_NAME = 'SC_NPROCESSORS_ONLN'
+try:
+    CPU_COUNT = _cpu_count()
+except NotImplementedError:
+    if CPU_COUNT_SYSCONF_NAME in os.sysconf_names:
+        CPU_COUNT = os.sysconf(CPU_COUNT_SYSCONF_NAME)
+    else:
+        CPU_COUNT = 2
+
+class DummyPool(object):
+    """A dummy pool object which implements the API we need."""
+    def __init__(self, processes=0):
+        self.processes = processes
+
+    def imap(self, func, iterable):
+        return imap(func, iterable)
+
+def make_pool(processes=CPU_COUNT):
+    """Returns a pool object using the given number of `processes`."""
+    implementations = [GreenPool, MultiProcessingPool, DummyPool]
+    for implementation in implementations:
+        if implementation is not None:
+            return implementation(processes)

File tests/test_multiprocessingimpl.py

+# -*- coding: utf-8 -*-
+"""
+    test_multiprocessingimpl
+    ~~~~~~~~~~~~~~~~~~~~~~~~
+
+    :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+from sphinx.util.multiprocessingimpl import DummyPool, make_pool
+
+def test_dummy_pool():
+    pool = DummyPool()
+    pool = DummyPool(2)
+    values = range(10)
+    for i, result in enumerate(pool.imap(lambda x: x, values)):
+        assert result == values[i]
+
+def test_make_pool():
+    pool = make_pool()
+    assert getattr(pool, 'imap', False)