How to reuse session and client in a more complex app ?
See original GitHub issue- Async AWS SDK for Python version: 9.0.0
- Python version: 3.7
- Operating System: Lambda (Linux)
Description
I would like to reuse session or client in multiple concurrent calls to S3 and SQS. This is in order to have faster execution of the code. If I build the session and client on each call then it takes too much time. I tried creating the client globally in the file where all aws functions are. I also tried creating it in the main file and passing it as a parameter to the function that would use it.
What I Did
Here is a minimalistic code of my app. The complete code will span in multiple files and functions. All aws functions are in separate file from the logic that creates the loops and lists of tasks needed to be run (so I cannot create all my logic under one async with aioboto3.Session().client('s3') as s3: .
import asyncio
from io import BytesIO
import aioboto3
s3_client = aioboto3.Session().client('s3')
def lambda_handler(a,b):
asyncio.run(main())
async def main():
filelike1 = BytesIO()
filelike2 = BytesIO()
await asyncio.wait([s3_get(filelike1),s3_get(filelike2)])
# later or maybe in some other function
await asyncio.wait([s3_get(filelike3),s3_get(filelike3)])
async def s3_get(filelike):
async with s3_client as s3: # to make it work here i create new session and client async with aioboto3.Session().client('s3') as s3:
return await s3.download_fileobj('s3-files-003', '.versions.txt', filelike)
future: <Task finished coro=<s3_get() done, defined at /var/task/lambda_function.py:19> exception=RuntimeError('cannot reuse already awaited coroutine')>
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 20, in s3_get
async with s3_client as s3:
File "/opt/python/lib/python3.7/site-packages/aiobotocore/session.py", line 37, in __aenter__
self._client = await self._coro
RuntimeError: cannot reuse already awaited coroutine
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
New NSURLSession for every DataTask overkill?
Creating a session per request is inefficient both on the CPU and, more importantly, on the network. Specifically, it prevents connection reuse, ...
Read more >Session Management - OWASP Cheat Sheet Series
A web session is a sequence of network HTTP request and response transactions associated with the same user. Modern and complex web applications...
Read more >Do sessions really violate RESTfulness? - Stack Overflow
The idea behind statelessness is that the SERVER is stateless, not the clients. Any client issuing an identical request (same headers, cookies, URI,...
Read more >Configure authentication session management - Microsoft Entra
Customize Azure AD authentication session configuration including user sign-in frequency and browser session persistence.
Read more >Enable feature reuse across accounts and teams using ...
As organizations build data-driven applications using ML, ... new enhanced features that can help when building more complex ML models.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok I’ve looked into it, reusing the session can be done, but reusing the output of session.client(‘s3’) etc… doesnt make sense due to how Chalice and by extension lambda works.
I’ve released a beta version
aioboto3==9.2.0b0which has an example integration which might provide useful. Example use here - https://github.com/terrycain/aioboto3/blob/master/tests/chalice_app/__init__.py Bit of info here - https://aioboto3.readthedocs.io/en/latest/chalice.htmlCurrently only works for http endpoints. I’ve not tested it deploying to an actual lambda, but it seems to work fine locally. Give it a go, if it works for HTTP and you find it useful, I can look at expanding it to work with the rest of the event types.
I tried storing the client in the app’s context but the same issue happens :
If I use the session then it works but I have to create a client for each call.