|
3 | 3 | """ |
4 | 4 | from typing import Dict, Sequence, Set, Type, Union, cast |
5 | 5 |
|
| 6 | +from joblib import Parallel, delayed, parallel_config # type: ignore |
| 7 | + |
6 | 8 | from ..application.ports import caching |
7 | 9 | from ..application.ports.filesystem import AbstractFileSystem |
8 | 10 | from ..application.ports.graph import ImportGraph |
@@ -137,11 +139,22 @@ def _scan_packages( |
137 | 139 | else: |
138 | 140 | imports_by_module_file[module_file] = direct_imports |
139 | 141 |
|
140 | | - # TODO Parallelise this part. |
141 | | - for module_file in modules_files_to_scan.difference(imports_by_module_file): |
142 | | - imports_by_module_file[module_file] = import_scanner.scan_for_imports( |
143 | | - module_file.module, exclude_type_checking_imports=exclude_type_checking_imports |
144 | | - ) |
| 142 | + remaining_modules_files_to_scan = list( |
| 143 | + modules_files_to_scan.difference(imports_by_module_file) |
| 144 | + ) |
| 145 | + # n_jobs=-1 will create one job per CPU. |
| 146 | + with parallel_config(n_jobs=-1): |
| 147 | + for module_file, direct_imports in zip( |
| 148 | + remaining_modules_files_to_scan, |
| 149 | + Parallel()( |
| 150 | + delayed(import_scanner.scan_for_imports)( |
| 151 | + module_file.module, |
| 152 | + exclude_type_checking_imports=exclude_type_checking_imports, |
| 153 | + ) |
| 154 | + for module_file in remaining_modules_files_to_scan |
| 155 | + ), |
| 156 | + ): |
| 157 | + imports_by_module_file[module_file] = direct_imports |
145 | 158 |
|
146 | 159 | imports_by_module: Dict[Module, Set[DirectImport]] = { |
147 | 160 | k.module: v for k, v in imports_by_module_file.items() |
|
0 commit comments