如何从aiohttp服务向SQSqueue发送消息?

2024-04-20 10:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从aiohttp服务发送消息到SQS队列。 我阅读了aiobotocore的文档和所有示例,但是我没有看到任何关于如何为example从aipg发送与postgres相同的消息的内容。你知道吗

async def pg_engine(app):
    app['pg_engine'] = await create_engine(
        user='postgre',
        database='postgre',
        host='localhost',
        port=5432,
        password=''
    )
    yield
    app['pg_engine'].close()
    await app['pg_engine'].wait_closed()

app.cleanup_ctx.append(pg_engine)

升级版 解决办法是

async def sqs_client(app):
    session = get_session(loop=app.loop)
    app['sqs'] = session.create_client('sqs',
                                       endpoint_url=SQS_QUEUE_URL,
                                       region_name=SQS_REGION)
    yield
    # close connection after finish
    await app['sqs'].close()

app.cleanup_ctx.append(sqs_client)

Tags: clientapp消息closeasyncsessiondefcreate