-
Notifications
You must be signed in to change notification settings - Fork 1
⚡ Bolt: Avoid thread pool overhead for small rule updates #192
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 |
|---|---|---|
|
|
@@ -1172,23 +1172,36 @@ def process_batch(batch_idx: int, batch_data: List[str]) -> Optional[List[str]]: | |
|
|
||
| # Optimization 3: Parallelize batch processing | ||
| # Using 3 workers to speed up writes without hitting aggressive rate limits. | ||
| with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor: | ||
| futures = { | ||
| executor.submit(process_batch, i, batch): i | ||
| for i, batch in enumerate(batches, 1) | ||
| } | ||
| # If only 1 batch, run it synchronously to avoid ThreadPoolExecutor overhead. | ||
| if total_batches == 1: | ||
| result = process_batch(1, batches[0]) | ||
| if result: | ||
| successful_batches += 1 | ||
| existing_rules.update(result) | ||
|
|
||
| render_progress_bar( | ||
| successful_batches, | ||
| total_batches, | ||
| f"Folder {sanitize_for_log(folder_name)}", | ||
| ) | ||
| else: | ||
| with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor: | ||
| futures = { | ||
| executor.submit(process_batch, i, batch): i | ||
| for i, batch in enumerate(batches, 1) | ||
| } | ||
|
|
||
| for future in concurrent.futures.as_completed(futures): | ||
| result = future.result() | ||
| if result: | ||
| successful_batches += 1 | ||
| existing_rules.update(result) | ||
|
|
||
| render_progress_bar( | ||
| successful_batches, | ||
| total_batches, | ||
| f"Folder {sanitize_for_log(folder_name)}", | ||
| ) | ||
| for future in concurrent.futures.as_completed(futures): | ||
| result = future.result() | ||
| if result: | ||
| successful_batches += 1 | ||
| existing_rules.update(result) | ||
|
|
||
| render_progress_bar( | ||
| successful_batches, | ||
| total_batches, | ||
| f"Folder {sanitize_for_log(folder_name)}", | ||
| ) | ||
|
Comment on lines
+1176
to
+1204
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. There is code duplication between the To improve maintainability and follow the DRY (Don't Repeat Yourself) principle, you can extract the result handling logic into a local helper function. def _handle_batch_result(result: Optional[List[str]]):
nonlocal successful_batches
if result:
successful_batches += 1
existing_rules.update(result)
render_progress_bar(
successful_batches,
total_batches,
f"Folder {sanitize_for_log(folder_name)}",
)
if total_batches == 1:
result = process_batch(1, batches[0])
_handle_batch_result(result)
else:
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
futures = {
executor.submit(process_batch, i, batch): i
for i, batch in enumerate(batches, 1)
}
for future in concurrent.futures.as_completed(futures):
result = future.result()
_handle_batch_result(result) |
||
|
|
||
| if successful_batches == total_batches: | ||
| if USE_COLORS: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,102 @@ | ||||||
|
|
||||||
Check warningCode scanning / Pylint (reported by Codacy) Missing module docstring
Missing module docstring
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing module docstring
Missing module docstring
|
||||||
| import unittest | ||||||
| from unittest.mock import MagicMock, patch, ANY | ||||||
Check warningCode scanning / Prospector (reported by Codacy) Unused ANY imported from unittest.mock (unused-import)
Unused ANY imported from unittest.mock (unused-import)
Check noticeCode scanning / Pylint (reported by Codacy) Unused ANY imported from unittest.mock
Unused ANY imported from unittest.mock
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Unused ANY imported from unittest.mock
Unused ANY imported from unittest.mock
|
||||||
| from unittest.mock import MagicMock, patch, ANY | |
| from unittest.mock import MagicMock, patch |
Check warning
Code scanning / Pylint (reported by Codacy)
Import "import main" should be placed at the top of the module
Check warning
Code scanning / Prospector (reported by Codacy)
Import "import main" should be placed at the top of the module (wrong-import-position)
Check warning
Code scanning / Pylintpython3 (reported by Codacy)
Import "import main" should be placed at the top of the module
Check warning
Code scanning / Pylint (reported by Codacy)
Missing class docstring
Check warning
Code scanning / Pylintpython3 (reported by Codacy)
Missing class docstring
Check warning
Code scanning / Pylint (reported by Codacy)
Attribute name "do" doesn't conform to snake_case naming style
Check warning
Code scanning / Pylintpython3 (reported by Codacy)
Attribute name "do" doesn't conform to snake_case naming style
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.
The mocks for the ThreadPoolExecutor instance and the commented-out development notes are unnecessary for this test. Since this test verifies the single-batch scenario where the thread pool is not used, these lines are redundant and can be removed. This will make the test cleaner and more focused on its primary assertion.
Check warning
Code scanning / Pylint (reported by Codacy)
Line too long (106/100)
Check warning
Code scanning / Pylintpython3 (reported by Codacy)
Line too long (106/100)
Check warning
Code scanning / Prospector (reported by Codacy)
expected 2 blank lines after class or function definition, found 1 (E305)
Check notice
Code scanning / Remark-lint (reported by Codacy)
Warn when references to undefined definitions are found.