-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: Optimize psutil process iteration to lazily fetch expensive attributes #21
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2024-05-15 - psutil process iteration performance | ||
| **Learning:** Querying expensive attributes like `memory_info` and `status` for all processes in `psutil.process_iter` incurs significant overhead, especially on Windows. | ||
| **Action:** Only query basic attributes like `pid` and `name` initially, and lazily fetch expensive attributes wrapped in try/except blocks only for the specific target processes. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -852,19 +852,32 @@ def _default_processes() -> list[dict[str, Any]]: | |
| if psutil is None: | ||
| return [] | ||
| processes = [] | ||
| for proc in psutil.process_iter(["pid", "name", "memory_info", "status"]): | ||
| # OPTIMIZATION: Only fetch basic attributes initially to avoid expensive calls for all processes | ||
| for proc in psutil.process_iter(["pid", "name"]): | ||
| try: | ||
| info = proc.info | ||
| except (psutil.NoSuchProcess, psutil.AccessDenied): | ||
| continue | ||
| if str(info.get("name") or "").casefold() != "excel.exe": | ||
| continue | ||
|
|
||
| # Lazily fetch expensive attributes only for target processes | ||
| try: | ||
| memory_info = proc.memory_info() | ||
| except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess, AttributeError): | ||
| memory_info = None | ||
|
|
||
| try: | ||
| status = proc.status() | ||
| except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess, AttributeError): | ||
| status = "running" | ||
|
Comment on lines
+871
to
+873
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.
In the cockpit guard, Useful? React with πΒ / π. |
||
|
|
||
| processes.append( | ||
| { | ||
| "pid": info.get("pid"), | ||
| "name": info.get("name"), | ||
| "memory_info": info.get("memory_info"), | ||
| "status": info.get("status"), | ||
| "memory_info": memory_info, | ||
| "status": status, | ||
| } | ||
| ) | ||
| return processes | ||
|
|
||
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.
When
proc.status()raisesNoSuchProcess/ZombieProcess, this code now falls back to"running"and still records the PID. That creates ghost Excel entries when a process exits betweenproc.infoand the lazy attribute reads, soget_running_instances()can briefly report non-existent sessions with0.0MB memory. Previously, a process disappearing during the scan path was skipped by the outer exception handling.Useful? React with πΒ / π.