From 3bd70047339d84fe857720b2d4d67485e969aa3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 4 Nov 2020 11:44:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20SWU:=20Improve=20pip=20cal?= =?UTF-8?q?ler=20caching=20behaviour?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to synchronize so we don't try to determine the pip caller again and again for all registered checks that are being processed concurrently. --- .../plugins/softwareupdate/updaters/pip.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/octoprint/plugins/softwareupdate/updaters/pip.py b/src/octoprint/plugins/softwareupdate/updaters/pip.py index 106033dcf..4a6e234ff 100644 --- a/src/octoprint/plugins/softwareupdate/updaters/pip.py +++ b/src/octoprint/plugins/softwareupdate/updaters/pip.py @@ -6,7 +6,9 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agp __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License" +import collections import logging +import threading import pkg_resources @@ -25,6 +27,7 @@ _POTENTIAL_EGG_PROBLEM_POSIX = "No such file or directory" _POTENTIAL_EGG_PROBLEM_WINDOWS = "The system cannot find the file specified" _pip_callers = {} +_pip_caller_mutex = collections.defaultdict(threading.RLock) _pip_version_dependency_links = pkg_resources.parse_version("1.5") @@ -44,17 +47,21 @@ def can_perform_update(target, check, online=True): def _get_pip_caller(command=None): + global _pip_callers + global _pip_caller_mutex + key = command if command is None: key = "__default" - if key not in _pip_callers: - try: - _pip_callers[key] = create_pip_caller(command=command) - except UnknownPip: - _pip_callers[key] = None + with _pip_caller_mutex[key]: + if key not in _pip_callers: + try: + _pip_callers[key] = create_pip_caller(command=command) + except UnknownPip: + pass - return _pip_callers[key] + return _pip_callers.get(key) def perform_update(target, check, target_version, log_cb=None, online=True, force=False):