Skip to content

Commit 1ebc727

Browse files
[aiohttp] - disable reset query and remove explicit call prepare (TechEmpower#9829)
* [aiohttp] - disable reset query and remove explicit call prepare * revert import change Co-authored-by: Sam Bull <[email protected]> --------- Co-authored-by: Sam Bull <[email protected]>
1 parent be13fbf commit 1ebc727

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

frameworks/Python/aiohttp/app/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ def pg_dsn(dialect=None) -> str:
3737
)
3838
return url.render_as_string(hide_password=False)
3939

40+
class NoResetConnection(asyncpg.Connection):
41+
__slots__ = ()
42+
43+
def get_reset_query(self):
44+
return ''
4045

4146
async def db_ctx(app: web.Application):
4247
# number of gunicorn workers = multiprocessing.cpu_count() as per gunicorn_conf.py
@@ -52,7 +57,7 @@ async def db_ctx(app: web.Application):
5257
app['db_session'] = async_sessionmaker(engine)
5358
else:
5459
dsn = pg_dsn()
55-
app['pg'] = await asyncpg.create_pool(dsn=dsn, min_size=min_size, max_size=max_size, loop=app.loop)
60+
app['pg'] = await asyncpg.create_pool(dsn=dsn, min_size=min_size, max_size=max_size, loop=app.loop, connection_class=NoResetConnection)
5661

5762
yield
5863

frameworks/Python/aiohttp/app/views.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async def single_database_query_raw(request):
6666
id_ = randint(1, 10000)
6767

6868
async with request.app['pg'].acquire() as conn:
69-
r = await conn.fetchval('SELECT id,randomnumber FROM world WHERE id = $1', id_)
69+
r = await conn.fetchval(READ_ROW_SQL, id_)
7070
return json_response({'id': id_, 'randomNumber': r})
7171

7272

@@ -96,11 +96,10 @@ async def multiple_database_queries_raw(request):
9696

9797
result = []
9898
async with request.app['pg'].acquire() as conn:
99-
stmt = await conn.prepare(READ_ROW_SQL)
10099
for id_ in ids:
101100
result.append({
102101
'id': id_,
103-
'randomNumber': await stmt.fetchval(id_),
102+
'randomNumber': await conn.fetchval(READ_ROW_SQL, id_),
104103
})
105104
return json_response(result)
106105

@@ -160,10 +159,9 @@ async def updates_raw(request):
160159
worlds = [{'id': row_id, 'randomNumber': number} for row_id, number in updates]
161160

162161
async with request.app['pg'].acquire() as conn:
163-
stmt = await conn.prepare(READ_ROW_SQL)
164162
for id_, _ in updates:
165163
# the result of this is the int previous random number which we don't actually use
166-
await stmt.fetchval(id_)
164+
await conn.fetchval(READ_ROW_SQL, id_)
167165
await conn.executemany(WRITE_ROW_SQL, updates)
168166

169167
return json_response(worlds)

0 commit comments

Comments
 (0)