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"""
6import os
7import warnings
8
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 as mp
17 import _multiprocessing # noqa
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(
36 os.getpid(), next(_rand))
37 _sem = SemLock(0, 0, 1, name=name, unlink=True)
38 del _sem # cleanup
39 break
40 except FileExistsError as e: # pragma: no cover
41 if i >= 99:
42 raise FileExistsError(
43 'cannot find name for semaphore') from e
44 except (FileExistsError, AttributeError, ImportError, OSError) as e:
45 mp = None
46 warnings.warn('%s. joblib will operate in serial mode' % (e,))
47
48
49# 3rd stage: backward compat for the assert_spawning helper
50if mp is not None:
51 from multiprocessing.context import assert_spawning
52else:
53 assert_spawning = None