KEMBAR78
gh-112278: reduce time cost for platform module when no permission to WMI on Windows by aisk · Pull Request #112658 · python/cpython · GitHub
Skip to content

Conversation

@aisk
Copy link
Contributor

@aisk aisk commented Dec 3, 2023

Using an Event object to simulate timeout for IWbemLocator::ConnectServer. I set it to 100ms, and it's enough for the normal path on my machine. And there have another 100ms plus for waiting the query thread. The final time usage on my local machine:

PS C:\Users\xxxxx\Source\cpython> .\python.bat -m timeit -s "import platform" -n 1 -r 1 "platform.system()"
Running Debug|x64 interpreter...
1 loop, best of 1: 301 msec per loop

Lib/platform.py Outdated
Comment on lines 332 to 335
global _wmi_query
def _wmi_query(table, *keys):
raise OSError("not supported")
raise OSError("not supported")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's restructure this import block to make _wmi = None when it fails to import, and then if we fail to query it can clear out _wmi and subsequent calls will act as if it were never imported.

@aisk
Copy link
Contributor Author

aisk commented Dec 5, 2023

Updated, ready for review.

@aisk aisk requested a review from zooba December 6, 2023 15:10
@zooba zooba merged commit a955fd6 into python:main Dec 7, 2023
@zooba
Copy link
Member

zooba commented Dec 7, 2023

Will add the backport to 3.12 after 3.12.1 has settled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants