Skip to content

Commit a4775c3

Browse files
authored
[Python] Bump Granian to 2.4 (#9979)
1 parent 3365c78 commit a4775c3

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

frameworks/Python/granian/app_asgi.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import os
32

43
from operator import itemgetter
@@ -10,6 +9,15 @@
109
import jinja2
1110
import orjson
1211

12+
PG_POOL_SIZE = 2
13+
14+
15+
class NoResetConnection(asyncpg.Connection):
16+
__slots__ = ()
17+
18+
def get_reset_query(self):
19+
return ""
20+
1321

1422
async def pg_setup():
1523
global pool
@@ -18,7 +26,10 @@ async def pg_setup():
1826
password=os.getenv('PGPASS', 'benchmarkdbpass'),
1927
database='hello_world',
2028
host='tfb-database',
21-
port=5432
29+
port=5432,
30+
min_size=PG_POOL_SIZE,
31+
max_size=PG_POOL_SIZE,
32+
connection_class=NoResetConnection,
2233
)
2334

2435

@@ -99,10 +110,9 @@ async def route_queries(scope, receive, send):
99110

100111
async with pool.acquire() as connection:
101112
statement = await connection.prepare(SQL_SELECT)
102-
for row_id in row_ids:
103-
number = await statement.fetchval(row_id)
104-
worlds.append({'id': row_id, 'randomNumber': number})
113+
rows = await statement.fetchmany([(v,) for v in row_ids])
105114

115+
worlds = [{'id': row_id, 'randomNumber': number[0]} for row_id, number in zip(row_ids, rows)]
106116
await send(JSON_RESPONSE)
107117
await send({
108118
'type': 'http.response.body',
@@ -136,8 +146,7 @@ async def route_updates(scope, receive, send):
136146

137147
async with pool.acquire() as connection:
138148
statement = await connection.prepare(SQL_SELECT)
139-
for row_id, _ in updates:
140-
await statement.fetchval(row_id)
149+
await statement.executemany([(i[0],) for i in updates])
141150
await connection.executemany(SQL_UPDATE, updates)
142151

143152
await send(JSON_RESPONSE)

frameworks/Python/granian/app_rsgi.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
import jinja2
1010
import orjson
1111

12+
PG_POOL_SIZE = 2
13+
14+
15+
class NoResetConnection(asyncpg.Connection):
16+
__slots__ = ()
17+
18+
def get_reset_query(self):
19+
return ""
20+
1221

1322
async def pg_setup():
1423
global pool
@@ -17,7 +26,10 @@ async def pg_setup():
1726
password=os.getenv('PGPASS', 'benchmarkdbpass'),
1827
database='hello_world',
1928
host='tfb-database',
20-
port=5432
29+
port=5432,
30+
min_size=PG_POOL_SIZE,
31+
max_size=PG_POOL_SIZE,
32+
connection_class=NoResetConnection,
2133
)
2234

2335

@@ -77,10 +89,9 @@ async def route_queries(scope, proto):
7789

7890
async with pool.acquire() as connection:
7991
statement = await connection.prepare(SQL_SELECT)
80-
for row_id in row_ids:
81-
number = await statement.fetchval(row_id)
82-
worlds.append({'id': row_id, 'randomNumber': number})
92+
rows = await statement.fetchmany([(v,) for v in row_ids])
8393

94+
worlds = [{'id': row_id, 'randomNumber': number[0]} for row_id, number in zip(row_ids, rows)]
8495
proto.response_bytes(
8596
200,
8697
JSON_HEADERS,
@@ -112,8 +123,7 @@ async def route_updates(scope, proto):
112123

113124
async with pool.acquire() as connection:
114125
statement = await connection.prepare(SQL_SELECT)
115-
for row_id, _ in updates:
116-
await statement.fetchval(row_id)
126+
await statement.executemany([(i[0],) for i in updates])
117127
await connection.executemany(SQL_UPDATE, updates)
118128

119129
proto.response_bytes(
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
asyncpg==0.29.0
2-
granian[uvloop]>=2.2.0,<2.3.0
3-
jinja2==3.1.4
4-
orjson==3.10.2
1+
asyncpg==0.30.0
2+
granian[uvloop]>=2.4.0,<2.5.0
3+
jinja2==3.1.6
4+
orjson==3.10.16

0 commit comments

Comments
 (0)