Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQS issue with aioboto3.session.client('SQS') #279

Closed
Dz0n1Pr0 opened this issue Oct 25, 2022 · 4 comments
Closed

SQS issue with aioboto3.session.client('SQS') #279

Dz0n1Pr0 opened this issue Oct 25, 2022 · 4 comments

Comments

@Dz0n1Pr0
Copy link

Hello, I have been working on a function that will log my responses asynchronously to SQS, but I am having trouble creating an SQS client that is open and not closed like how it is shown in the "with aioboto3.session.client("sqs") as sqs" example.

I tried following the ideas in this issue:
https://github.com/terrycain/aioboto3/issues/233

I created a function

app.session = aioboto3.Session()
app.sqs = None

async def get_sqs():
if app.sqs is None:
app.sqs = await app.session.client("sqs")
return app.sqs

but when I call the function I get an error TypeError: object ClientCreatorContext can't be used in 'await' expression on line app.sqs = await app.session.client("sqs")

Can you possibly assist me in resolving this issue?

I am using Quart for developing this API.

@BrutalSimplicity
Copy link

I think this is mentioned in the README.

The .client and .resource functions must now be used as async context managers.

Looks like you just need to use a context manager like in the README examples.

@Dz0n1Pr0
Copy link
Author

Dz0n1Pr0 commented Nov 9, 2022

The issue is that each time I want to send logs to SQS it takes too long for every request (and we have hundreds per hour) to open a new SQS client connection.
Also, I think we can agree that it is not practical to open It every time if there is a way for me to keep this one connection to SQS open as long as my API is available.

Based on the ticket I linked in my first comment I thought this approach was viable.

@terricain
Copy link
Owner

Yeah you definitely dont want to be creating a new client for every message.

You should be able to do something like this: (untested and probably not syntactically correct)

import contextlib
import asyncio
import aioboto3

async def main():
    stack = contextlib.AsyncExitStack()
    session = aioboto3.Session()
    sqs = await stack.enter_async_context(session.client("sqs"))
    #... pass sqs object around, into other async functions etc..

    # before you exit, clean up
    await stack.aclose()


if __name__ == '__main__':
    asyncio.run(main())

That or you could just do something like

import asyncio
import aioboto3

SQS = None

async def main():
    global SQS
    session = aioboto3.Session()
    SQS = session.client("sqs").__aenter__()
    #... pass sqs object around, into other async functions etc..

if __name__ == '__main__':
    asyncio.run(main())

If you really dont care about cleaning up.

@Dz0n1Pr0
Copy link
Author

I managed to make it work with the second solution, where I awaited the SQS object that I got through the aenter() function as it was a corutine. Thank you for the assistance :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants