Skip to content

Commit 3877be3

Browse files
authored
fastwsgi: Update asyncpg and using methods for async tasks (#9998)
1 parent 890ba36 commit 3877be3

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

frameworks/Python/fastwsgi/app-asgi.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,25 @@
1717

1818
db_pool = None
1919

20+
PG_POOL_SIZE = 4
21+
22+
class NoResetConnection(asyncpg.Connection):
23+
__slots__ = ()
24+
25+
def get_reset_query(self):
26+
return ""
27+
2028
async def db_setup():
2129
global db_pool
2230
db_pool = await asyncpg.create_pool(
2331
user=os.getenv('PGUSER', 'benchmarkdbuser'),
2432
password=os.getenv('PGPASS', 'benchmarkdbpass'),
2533
database='hello_world',
2634
host='tfb-database',
27-
port=5432
35+
port=5432,
36+
min_size=PG_POOL_SIZE,
37+
max_size=PG_POOL_SIZE,
38+
connection_class=NoResetConnection,
2839
)
2940

3041
READ_ROW_SQL = 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1'
@@ -102,15 +113,10 @@ async def multiple_database_queries(scope, receive, send):
102113
row_ids = random.sample(range(1, 10000), num_queries)
103114
worlds = [ ]
104115

105-
db_conn = await db_pool.acquire()
106-
try:
107-
statement = await db_conn.prepare(READ_ROW_SQL)
108-
for row_id in row_ids:
109-
number = await statement.fetchval(row_id)
110-
worlds.append( {'id': row_id, 'randomNumber': number} )
111-
finally:
112-
await db_pool.release(db_conn)
116+
async with db_pool.acquire() as db_conn:
117+
rows = await db_conn.fetchmany(READ_ROW_SQL, [ (v, ) for v in row_ids ] )
113118

119+
worlds = [ { 'id': row_id, 'randomNumber': number[0] } for row_id, number in zip(row_ids, rows) ]
114120
content = jsonify(worlds)
115121
await send(JSON_RESPONSE)
116122
await send({
@@ -154,14 +160,9 @@ async def database_updates(scope, receive, send):
154160

155161
worlds = [ {"id": row_id, "randomNumber": number} for row_id, number in updates ]
156162

157-
db_conn = await db_pool.acquire()
158-
try:
159-
statement = await db_conn.prepare(READ_ROW_SQL)
160-
for row_id, _ in updates:
161-
await statement.fetchval(row_id)
163+
async with db_pool.acquire() as db_conn:
164+
await db_conn.executemany(READ_ROW_SQL, [ (i[0], ) for i in updates ] )
162165
await db_conn.executemany(WRITE_ROW_SQL, updates)
163-
finally:
164-
await db_pool.release(db_conn)
165166

166167
content = jsonify(worlds)
167168
await send(JSON_RESPONSE)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
click==8.0.1
2-
ujson==5.7.0
2+
ujson==5.10.0
33
#fastwsgi==0.0.9
4-
git+https://github.com/jamesroberts/fastwsgi.git@5572bb31b859d690be225707b9e7e25af397544b
5-
asyncpg==0.27.0
6-
Jinja2==3.1.6
4+
git+https://github.com/jamesroberts/fastwsgi.git@eb6e60babab73e769f49c183cfc46bb270ca84bd
5+
asyncpg==0.30.0
6+
jinja2==3.1.6
77
cachetools==5.3.0
88
asyncache==0.3.1

0 commit comments

Comments
 (0)