From 79be8bc5ad728c249e9e4877bae42b97f91ddff7 Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Thu, 12 Aug 2021 15:57:32 +0100 Subject: [PATCH] ssh: Memoize the SshConnection._get_sftp() Since we have the guarantee to have a different SshConnection per thread, we can memoize paramiko's SFTPClient. This provides a great performance boost. --- devlib/utils/ssh.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/devlib/utils/ssh.py b/devlib/utils/ssh.py index 3cff55c..4f7fbc1 100644 --- a/devlib/utils/ssh.py +++ b/devlib/utils/ssh.py @@ -31,6 +31,7 @@ import contextlib import weakref import select import copy +import functools from pipes import quote from future.utils import raise_from @@ -406,6 +407,7 @@ class SshConnection(SshConnectionBase): def _get_progress_cb(self): return self.transfer_mgr.progress_cb if self.transfer_mgr is not None else None + @functools.lru_cache() def _get_sftp(self, timeout): try: sftp = self.client.open_sftp() @@ -417,6 +419,7 @@ class SshConnection(SshConnectionBase): sftp.get_channel().settimeout(timeout) return sftp + @functools.lru_cache() def _get_scp(self, timeout): return SCPClient(self.client.get_transport(), socket_timeout=timeout, progress=self._get_progress_cb())