aio-libs / aio-libs/aiopg

How to share connections to the same host with different databases?

未关闭
#503 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
1.4k
派生
170
PR 合并指标
30 天内没有已合并 PR

描述

I want to be able to share the same connection to a PG server host among different databases. Is it possible to implement such use case with aiopg?

import asyncio
import aiopg

dsn = 'user=aiopg password=passwd host=127.0.0.1'

async def go(queries):
results = []
async with aiopg.create_pool(dsn) as pool:
for dbname, dbqueries in queries.items():
async with pool.acquire() as conn:

conn.set_database(dbname=dbname) # I want this!!

async with conn.cursor() as cur:
for query in dbqueries:
await cur.execute(query)
ret = []
async for row in cur:
ret.append(row)
result.append(ret)

with open('/tmp/bigfile.json') as fh:
queries = json.load(fh)

loop = asyncio.get_event_loop()
loop.run_until_complete(go(queries))

When we are using TLS connections to a PG server the cost to keep opened multiple connections to different database is high. So I'd like to implement database switching to share the connections from a pool with different databases.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。