Skip to content

Commit

Permalink
lazy-load boto3 to save ~400ms startup time (#12914)
Browse files Browse the repository at this point in the history
* lazy-load boto3 to save ~400ms startup time

Co-authored-by: Ken Odegard <kodegard@anaconda.com>

---------

Co-authored-by: Ken Odegard <kodegard@anaconda.com>
  • Loading branch information
dholth and kenodegard committed Jul 24, 2023
1 parent 14d611a commit bf5876c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
31 changes: 20 additions & 11 deletions conda/gateways/connection/adapters/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions news/lazy-load-boto3
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* Import boto3 only when s3 channels are used, saving startup time. (#12914)

### Bug fixes

* <news item>

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>

0 comments on commit bf5876c

Please sign in to comment.