|
16 | 16 | import py_compile
|
17 | 17 | import struct
|
18 | 18 |
|
19 |
| -try: |
20 |
| - from concurrent.futures import ProcessPoolExecutor |
21 |
| -except ImportError: |
22 |
| - ProcessPoolExecutor = None |
| 19 | +# Only import when needed, as low resource platforms may fail to import it |
| 20 | +ProcessPoolExecutor = None |
| 21 | + |
23 | 22 | from functools import partial
|
24 | 23 |
|
25 | 24 | __all__ = ["compile_dir","compile_file","compile_path"]
|
@@ -70,13 +69,21 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
|
70 | 69 | workers: maximum number of parallel workers
|
71 | 70 | invalidation_mode: how the up-to-dateness of the pyc will be checked
|
72 | 71 | """
|
73 |
| - if workers is not None and workers < 0: |
74 |
| - raise ValueError('workers must be greater or equal to 0') |
75 |
| - |
| 72 | + global ProcessPoolExecutor |
| 73 | + if workers is not None: |
| 74 | + if workers < 0: |
| 75 | + raise ValueError('workers must be greater or equal to 0') |
| 76 | + elif workers > 1 and ProcessPoolExecutor is None: |
| 77 | + try: |
| 78 | + from concurrent.futures import ProcessPoolExecutor as PPE |
| 79 | + except ImportError: |
| 80 | + ProcessPoolExecutor = False |
| 81 | + else: |
| 82 | + ProcessPoolExecutor = PPE |
76 | 83 | files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
|
77 | 84 | ddir=ddir)
|
78 | 85 | success = True
|
79 |
| - if workers is not None and workers != 1 and ProcessPoolExecutor is not None: |
| 86 | + if workers is not None and workers != 1 and ProcessPoolExecutor: |
80 | 87 | workers = workers or None
|
81 | 88 | with ProcessPoolExecutor(max_workers=workers) as executor:
|
82 | 89 | results = executor.map(partial(compile_file,
|
|
0 commit comments