Source

sphinx-multiprocessing / 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)