-
Notifications
You must be signed in to change notification settings - Fork 23
Use joblib for robust parallel import scanning #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,10 @@ | |
| """ | ||
|
|
||
| from typing import Dict, Sequence, Set, Type, Union, cast, Iterable, Collection | ||
| import multiprocessing | ||
| import math | ||
|
|
||
| import joblib # type: ignore | ||
|
|
||
| from ..application.ports import caching | ||
| from ..application.ports.filesystem import AbstractFileSystem | ||
| from ..application.ports.graph import ImportGraph | ||
|
|
@@ -21,7 +22,7 @@ class NotSupplied: | |
|
|
||
|
|
||
| # This is an arbitrary number, but setting it too low slows down our functional tests considerably. | ||
| MIN_NUMBER_OF_MODULES_TO_SCAN_USING_MULTIPROCESSING = 50 | ||
| MIN_NUMBER_OF_MODULES_TO_SCAN_USING_MULTIPLE_PROCESSES = 64 | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐼🐼🐼 My OCD => 64 is a power of two, just feels nicer
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure we should include this change here, at least not without something in the changelog as it could have an impact on end users. On balance I'd prefer to concentrate on fixing the issue without making another change at the same time. |
||
|
|
||
|
|
||
| def build_graph( | ||
|
|
@@ -228,19 +229,19 @@ def _create_chunks(module_files: Collection[ModuleFile]) -> tuple[tuple[ModuleFi | |
| module_files_tuple = tuple(module_files) | ||
|
|
||
| number_of_module_files = len(module_files_tuple) | ||
| n_chunks = _decide_number_of_of_processes(number_of_module_files) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo (should really have put that in a separate commit...) |
||
| n_chunks = _decide_number_of_processes(number_of_module_files) | ||
| chunk_size = math.ceil(number_of_module_files / n_chunks) | ||
|
|
||
| return tuple( | ||
| module_files_tuple[i * chunk_size : (i + 1) * chunk_size] for i in range(n_chunks) | ||
| ) | ||
|
|
||
|
|
||
| def _decide_number_of_of_processes(number_of_module_files: int) -> int: | ||
| if number_of_module_files < MIN_NUMBER_OF_MODULES_TO_SCAN_USING_MULTIPROCESSING: | ||
| # Don't incur the overhead of multiprocessing. | ||
| def _decide_number_of_processes(number_of_module_files: int) -> int: | ||
| if number_of_module_files < MIN_NUMBER_OF_MODULES_TO_SCAN_USING_MULTIPLE_PROCESSES: | ||
| # Don't incur the overhead of multiple processes. | ||
| return 1 | ||
| return min(multiprocessing.cpu_count(), number_of_module_files) | ||
| return min(joblib.cpu_count(), number_of_module_files) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| def _scan_chunks( | ||
|
|
@@ -257,20 +258,15 @@ def _scan_chunks( | |
| ) | ||
|
|
||
| number_of_processes = len(chunks) | ||
| if number_of_processes == 1: | ||
| # No need to spawn a process if there's only one chunk. | ||
| [chunk] = chunks | ||
| return _scan_chunk(import_scanner, exclude_type_checking_imports, chunk) | ||
|
Comment on lines
-260
to
-263
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this special
|
||
| else: | ||
| with multiprocessing.Pool(number_of_processes) as pool: | ||
| imports_by_module_file: Dict[ModuleFile, Set[DirectImport]] = {} | ||
| import_scanning_jobs = pool.starmap( | ||
| _scan_chunk, | ||
| [(import_scanner, exclude_type_checking_imports, chunk) for chunk in chunks], | ||
| ) | ||
| for chunk_imports_by_module_file in import_scanning_jobs: | ||
| imports_by_module_file.update(chunk_imports_by_module_file) | ||
| return imports_by_module_file | ||
| import_scanning_jobs = joblib.Parallel(n_jobs=number_of_processes)( | ||
| joblib.delayed(_scan_chunk)(import_scanner, exclude_type_checking_imports, chunk) | ||
| for chunk in chunks | ||
| ) | ||
|
|
||
| imports_by_module_file = {} | ||
| for chunk_imports_by_module_file in import_scanning_jobs: | ||
| imports_by_module_file.update(chunk_imports_by_module_file) | ||
| return imports_by_module_file | ||
|
|
||
|
|
||
| def _scan_chunk( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MULTIPLE_PROCESSESinstead ofMULTIPROCESSINGto avoid confusion withmultiprocessingmodule