0

我希望 Faust 代理写入 PostgreSQL 表。我想使用 asyncpg 连接池,但找不到将其注入应用程序初始化代码的干净方法。

4

1 回答 1

0

只需将以下功能添加到您的 faust App

class KafkaWorker(faust.App):
    def __init__(self, *args: List, **kwargs: Dict) -> None:
        self.broker : str = kwargs.pop('broker')
        self._db_pool = None

        super().__init__(*args, broker=KafkaWorker._broker_faust_string(self.broker), **kwargs)

    async def db_pool(self) -> Pool:
        ''' '''
        if not self._db_pool:
            logging.warning('kafka.db_pool initialization...')
            self._db_pool = await db.db_pool()
            logging.warning('kafka.db_pool initialization...done ✓')

        return self._db_pool

db.db_pool 在哪里

from os import environ

import asyncpg
from asyncpg.pool import Pool
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base


session = scoped_session(sessionmaker())
Base = declarative_base()


async def db_pool() -> Pool:
    return await asyncpg.create_pool(
        dsn=environ.get('DB_CNX_STRING', 'postgresql://postgres:postgres@postgres:5432/actions')
    )

然后你访问它

@kafka.agent(actions_topic)
async def store_actions(actions: StreamT):
    async for action in actions:
        db_pool = await current_agent().app.db_pool()
        async with db_pool.acquire() as conn:
            try:
                yield await save_action(conn, action.to_representation())
            except StoreException:
                logger.exception(f'Error while inserting action in DB, continuing....')
            finally:
                yield action.id
于 2020-04-01T16:10:41.630 回答