aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/python/pythonutils.cpp
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@qt.io>2024-12-12 11:50:31 +0100
committerDavid Schulz <david.schulz@qt.io>2025-01-07 13:15:59 +0000
commit94c43600852a0d018cb82f46c6549960f182dca8 (patch)
tree2bc2b8d9a6bb60ffdc829c2f72630d0ccad8f81b /src/plugins/python/pythonutils.cpp
parent5fa27341fec78c98966b5b5cae97f93824309278 (diff)
Python: optimize the pip and venv checks
Those checks can take an considerable amount of time. So instead of running them the first time we want to check this info run them in the background as soon as we have loaded the python settings. Change-Id: I287acd2a5fd7b053873257238f7dfbaa9cf00170 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src/plugins/python/pythonutils.cpp')
-rw-r--r--src/plugins/python/pythonutils.cpp39
1 files changed, 23 insertions, 16 deletions
diff --git a/src/plugins/python/pythonutils.cpp b/src/plugins/python/pythonutils.cpp
index 04144a3e41b..efcfc4940a5 100644
--- a/src/plugins/python/pythonutils.cpp
+++ b/src/plugins/python/pythonutils.cpp
@@ -20,6 +20,7 @@
#include <utils/algorithm.h>
#include <utils/mimeutils.h>
#include <utils/qtcprocess.h>
+#include <utils/synchronizedvalue.h>
#include <QReadLocker>
@@ -192,31 +193,37 @@ bool isVenvPython(const FilePath &python)
return python.parentDir().parentDir().pathAppended("pyvenv.cfg").exists();
}
-static bool isUsableHelper(QHash<FilePath, bool> *cache, const QString &keyString,
- const QString &commandArg, const FilePath &python)
+static bool isUsableHelper(
+ SynchronizedValue<QHash<FilePath, bool>> *cache,
+ const QString &commandArg,
+ const FilePath &python)
{
- auto it = cache->find(python);
- if (it == cache->end()) {
- const Key key = keyFromString(keyString);
- Process process;
- process.setCommand({python, {"-m", commandArg, "-h"}});
- process.runBlocking();
- const bool usable = process.result() == ProcessResult::FinishedWithSuccess;
- it = cache->insert(python, usable);
- }
- return *it;
+ std::optional<bool> result;
+ cache->read([&result, python](const QHash<FilePath, bool> &cache) {
+ if (auto it = cache.find(python); it != cache.end())
+ result = it.value();
+ });
+ if (result)
+ return *result;
+
+ Process process;
+ process.setCommand({python, {"-m", commandArg, "-h"}});
+ process.runBlocking();
+ const bool usable = process.result() == ProcessResult::FinishedWithSuccess;
+ cache->writeLocked()->insert(python, usable);
+ return usable;
}
bool venvIsUsable(const FilePath &python)
{
- static QHash<FilePath, bool> cache;
- return isUsableHelper(&cache, "pyVenvIsUsable", "venv", python);
+ static SynchronizedValue<QHash<FilePath, bool>> cache;
+ return isUsableHelper(&cache, "venv", python);
}
bool pipIsUsable(const FilePath &python)
{
- static QHash<FilePath, bool> cache;
- return isUsableHelper(&cache, "pyPipIsUsable", "pip", python);
+ static SynchronizedValue<QHash<FilePath, bool>> cache;
+ return isUsableHelper(&cache, "pip", python);
}
QString pythonVersion(const FilePath &python)