Open Izacht13 opened 7 years ago
I found a way to get this working:
import asyncio
import asyncpg
import japronto
async def connect_db_pool(app):
asyncio.set_event_loop(app.loop)
db_pool = await asyncpg.create_pool(
host='HOST',
database='DB',
user='USER',
password='PASSWORD',
)
app.extend_request(lambda x: db_pool, name='db_pool', property=True)
async def handle(request):
power = int(request.match_dict.get('power', 10))
async with request.db_pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchval('select 2 ^ $1', power)
return request.Response(
text="2 ^ {} is {}".format(power, result))
app = japronto.Application()
app.loop.run_until_complete(connect_db_pool(app))
app.router.add_route('/{power}', handle)
app.router.add_route('/', handle)
app.run(debug=True)
Example code from https://magicstack.github.io/asyncpg/current/usage.html#connection-pools Only substituting aiohttp web for japronto.
Trace:
I actually ran into this issue when I tried to add asyncpg to my existing project, boiled it down to this text case and found it was a root issue.
Here is the offending code:
You'll see I had to rely on the closure because japranto's app object doesn't support item assignment.