1"""Helper module to factorize the conditional multiprocessing import logic 
    2 
    3We use a distinct module to simplify import statements and avoid introducing 
    4circular dependencies (for instance for the assert_spawning name). 
    5""" 
    6 
    7import os 
    8import warnings 
    9 
    10# Obtain possible configuration from the environment, assuming 1 (on) 
    11# by default, upon 0 set to None. Should instructively fail if some non 
    12# 0/1 value is set. 
    13mp = int(os.environ.get("JOBLIB_MULTIPROCESSING", 1)) or None 
    14if mp: 
    15    try: 
    16        import _multiprocessing  # noqa 
    17        import multiprocessing as mp 
    18    except ImportError: 
    19        mp = None 
    20 
    21# 2nd stage: validate that locking is available on the system and 
    22#            issue a warning if not 
    23if mp is not None: 
    24    try: 
    25        # try to create a named semaphore using SemLock to make sure they are 
    26        # available on this platform. We use the low level object 
    27        # _multiprocessing.SemLock to avoid spawning a resource tracker on 
    28        # Unix system or changing the default backend. 
    29        import tempfile 
    30        from _multiprocessing import SemLock 
    31 
    32        _rand = tempfile._RandomNameSequence() 
    33        for i in range(100): 
    34            try: 
    35                name = "/joblib-{}-{}".format(os.getpid(), next(_rand)) 
    36                _sem = SemLock(0, 0, 1, name=name, unlink=True) 
    37                del _sem  # cleanup 
    38                break 
    39            except FileExistsError as e:  # pragma: no cover 
    40                if i >= 99: 
    41                    raise FileExistsError("cannot find name for semaphore") from e 
    42    except (FileExistsError, AttributeError, ImportError, OSError) as e: 
    43        mp = None 
    44        warnings.warn("%s.  joblib will operate in serial mode" % (e,)) 
    45 
    46 
    47# 3rd stage: backward compat for the assert_spawning helper 
    48if mp is not None: 
    49    from multiprocessing.context import assert_spawning 
    50else: 
    51    assert_spawning = None