ソースを参照

[Python] Bump Granian to 2.4 (#9979)

Giovanni Barillari 2 ヶ月 前
コミット
a4775c31fd

+ 16 - 7
frameworks/Python/granian/app_asgi.py

@@ -1,4 +1,3 @@
-import asyncio
 import os
 
 from operator import itemgetter
@@ -10,6 +9,15 @@ import asyncpg
 import jinja2
 import orjson
 
+PG_POOL_SIZE = 2
+
+
+class NoResetConnection(asyncpg.Connection):
+    __slots__ = ()
+
+    def get_reset_query(self):
+        return ""
+
 
 async def pg_setup():
     global pool
@@ -18,7 +26,10 @@ async def pg_setup():
         password=os.getenv('PGPASS', 'benchmarkdbpass'),
         database='hello_world',
         host='tfb-database',
-        port=5432
+        port=5432,
+        min_size=PG_POOL_SIZE,
+        max_size=PG_POOL_SIZE,
+        connection_class=NoResetConnection,
     )
 
 
@@ -99,10 +110,9 @@ async def route_queries(scope, receive, send):
 
     async with pool.acquire() as connection:
         statement = await connection.prepare(SQL_SELECT)
-        for row_id in row_ids:
-            number = await statement.fetchval(row_id)
-            worlds.append({'id': row_id, 'randomNumber': number})
+        rows = await statement.fetchmany([(v,) for v in row_ids])
 
+    worlds = [{'id': row_id, 'randomNumber': number[0]} for row_id, number in zip(row_ids, rows)]
     await send(JSON_RESPONSE)
     await send({
         'type': 'http.response.body',
@@ -136,8 +146,7 @@ async def route_updates(scope, receive, send):
 
     async with pool.acquire() as connection:
         statement = await connection.prepare(SQL_SELECT)
-        for row_id, _ in updates:
-            await statement.fetchval(row_id)
+        await statement.executemany([(i[0],) for i in updates])
         await connection.executemany(SQL_UPDATE, updates)
 
     await send(JSON_RESPONSE)

+ 16 - 6
frameworks/Python/granian/app_rsgi.py

@@ -9,6 +9,15 @@ import asyncpg
 import jinja2
 import orjson
 
+PG_POOL_SIZE = 2
+
+
+class NoResetConnection(asyncpg.Connection):
+    __slots__ = ()
+
+    def get_reset_query(self):
+        return ""
+
 
 async def pg_setup():
     global pool
@@ -17,7 +26,10 @@ async def pg_setup():
         password=os.getenv('PGPASS', 'benchmarkdbpass'),
         database='hello_world',
         host='tfb-database',
-        port=5432
+        port=5432,
+        min_size=PG_POOL_SIZE,
+        max_size=PG_POOL_SIZE,
+        connection_class=NoResetConnection,
     )
 
 
@@ -77,10 +89,9 @@ async def route_queries(scope, proto):
 
     async with pool.acquire() as connection:
         statement = await connection.prepare(SQL_SELECT)
-        for row_id in row_ids:
-            number = await statement.fetchval(row_id)
-            worlds.append({'id': row_id, 'randomNumber': number})
+        rows = await statement.fetchmany([(v,) for v in row_ids])
 
+    worlds = [{'id': row_id, 'randomNumber': number[0]} for row_id, number in zip(row_ids, rows)]
     proto.response_bytes(
         200,
         JSON_HEADERS,
@@ -112,8 +123,7 @@ async def route_updates(scope, proto):
 
     async with pool.acquire() as connection:
         statement = await connection.prepare(SQL_SELECT)
-        for row_id, _ in updates:
-            await statement.fetchval(row_id)
+        await statement.executemany([(i[0],) for i in updates])
         await connection.executemany(SQL_UPDATE, updates)
 
     proto.response_bytes(

+ 4 - 4
frameworks/Python/granian/requirements.txt

@@ -1,4 +1,4 @@
-asyncpg==0.29.0
-granian[uvloop]>=2.2.0,<2.3.0
-jinja2==3.1.4
-orjson==3.10.2
+asyncpg==0.30.0
+granian[uvloop]>=2.4.0,<2.5.0
+jinja2==3.1.6
+orjson==3.10.16