From bf5876c23fb33c014846c554d10c16c175313539 Mon Sep 17 00:00:00 2001 From: Daniel Holth Date: Mon, 24 Jul 2023 13:00:06 -0400 Subject: [PATCH] lazy-load boto3 to save ~400ms startup time (#12914) * lazy-load boto3 to save ~400ms startup time Co-authored-by: Ken Odegard --------- Co-authored-by: Ken Odegard --- conda/gateways/connection/adapters/s3.py | 31 +++++++++++++++--------- news/lazy-load-boto3 | 19 +++++++++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 news/lazy-load-boto3 diff --git a/conda/gateways/connection/adapters/s3.py b/conda/gateways/connection/adapters/s3.py index 050db99ea9..9964453aca 100644 --- a/conda/gateways/connection/adapters/s3.py +++ b/conda/gateways/connection/adapters/s3.py @@ -5,18 +5,23 @@ from logging import LoggerAdapter, getLogger from tempfile import SpooledTemporaryFile -have_boto3 = have_boto = False -try: - import boto3 +boto3 = boto = None - have_boto3 = True -except ImportError: - try: - import boto - have_boto = True +def _load_boto3(): + """ + Import boto3 on demand only to save startup time. + """ + global boto3, boto + + try: + import boto3 except ImportError: - pass + try: + import boto + except ImportError: + pass + from ....common.compat import ensure_binary from ....common.url import url_to_s3_info @@ -36,9 +41,13 @@ def send( resp = Response() resp.status_code = 200 resp.url = request.url - if have_boto3: + + if not (boto3 or boto): + _load_boto3() + + if boto3: return self._send_boto3(boto3, resp, request) - elif have_boto: + elif boto: return self._send_boto(boto, resp, request) else: stderrlog.info( diff --git a/news/lazy-load-boto3 b/news/lazy-load-boto3 new file mode 100644 index 0000000000..6d6cfdc6be --- /dev/null +++ b/news/lazy-load-boto3 @@ -0,0 +1,19 @@ +### Enhancements + +* Import boto3 only when s3 channels are used, saving startup time. (#12914) + +### Bug fixes + +* + +### Deprecations + +* + +### Docs + +* + +### Other + +*