Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

Commit 923c09c

Browse files
authored
[python] Create new Fortune object in scope of request (#10652)
The new fortune object must be constructed in the scope of the request, not as a constant: > Within the scope of the request, a new Fortune object must be constructed and added to the list. https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#fortunes
1 parent 3a5962f commit 923c09c

File tree

16 files changed

+19
-36
lines changed

16 files changed

+19
-36
lines changed

frameworks/Python/aiohttp/app/views.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ def json_response(payload):
2121
content_type="application/json",
2222
)
2323

24-
ADDITIONAL_FORTUNE_ORM = Fortune(id=0, message='Additional fortune added at request time.')
25-
ADDITIONAL_FORTUNE_ROW = {'id': 0, 'message': 'Additional fortune added at request time.'}
2624
READ_ROW_SQL = 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1'
2725
READ_SELECT_ORM = select(World.randomnumber).where(World.id == bindparam("id"))
2826
READ_FORTUNES_ORM = select(Fortune.id, Fortune.message)
@@ -112,7 +110,7 @@ async def fortunes(request):
112110
async with request.app['db_session']() as sess:
113111
ret = await sess.execute(READ_FORTUNES_ORM)
114112
fortunes = ret.all()
115-
fortunes.append(ADDITIONAL_FORTUNE_ORM)
113+
fortunes.append(Fortune(id=0, message='Additional fortune added at request time.'))
116114
fortunes.sort(key=sort_fortunes_orm)
117115
content = template.render(fortunes=fortunes)
118116
return Response(text=content, content_type='text/html')
@@ -124,7 +122,7 @@ async def fortunes_raw(request):
124122
"""
125123
async with request.app['pg'].acquire() as conn:
126124
fortunes = await conn.fetch('SELECT * FROM Fortune')
127-
fortunes.append(ADDITIONAL_FORTUNE_ROW)
125+
fortunes.append({'id': 0, 'message': 'Additional fortune added at request time.'})
128126
fortunes.sort(key=sort_fortunes_raw)
129127
content = template.render(fortunes=fortunes)
130128
return Response(text=content, content_type='text/html')

frameworks/Python/aioworkers/pg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
READ_ROW_SQL = 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1'
1414
WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
15-
ADDITIONAL_ROW = [0, "Additional fortune added at request time."]
1615
sort_fortunes_key = itemgetter(1)
1716
logger = logging.getLogger(__name__)
1817

@@ -73,7 +72,7 @@ async def fortunes(context, request):
7372
async with context.pg.pool.acquire() as connection:
7473
fortunes = await connection.fetch("SELECT * FROM Fortune")
7574

76-
fortunes.append(ADDITIONAL_ROW)
75+
fortunes.append([0, "Additional fortune added at request time."])
7776
fortunes.sort(key=sort_fortunes_key)
7877
content = context.templates.fortune.render(fortunes=fortunes)
7978

frameworks/Python/blacksheep/app-socketify.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from psqlpy import ConnectionPoolBuilder
1313
READ_ROW_SQL = 'SELECT "randomnumber" FROM "world" WHERE id = $1'
1414
WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
15-
ADDITIONAL_ROW = [0, "Additional fortune added at request time."]
1615
CORE_COUNT = multiprocessing.cpu_count()
1716
MAX_DB_CONNECTIONS = 2000
1817

@@ -97,7 +96,7 @@ async def fortunes_test(request):
9796
fortunes_fetch = await connection.fetch("SELECT * FROM Fortune")
9897
# fortunes = fortunes_fetch.result()
9998
fortunes = [list(item.values()) for item in fortunes_fetch.result()]
100-
fortunes.append(ADDITIONAL_ROW)
99+
fortunes.append([0, "Additional fortune added at request time."])
101100
fortunes.sort(key=lambda row: row[1])
102101
data = fortune_template.render(fortunes=fortunes)
103102
return bs.html(data)
@@ -138,4 +137,4 @@ def create_fork():
138137

139138
for i in range(1, workers):
140139
create_fork()
141-
run_app()
140+
run_app()

frameworks/Python/blacksheep/app.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
READ_ROW_SQL = 'SELECT "id", "randomnumber" FROM "world" WHERE id = $1'
1818
WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
19-
ADDITIONAL_ROW = [0, "Additional fortune added at request time."]
2019
MAX_CONNECTIONS = 1900
2120
CORE_COUNT = multiprocessing.cpu_count()
2221
MAX_POOL_SIZE = max(1,int(os.getenv('MAX_POOL_SIZE', MAX_CONNECTIONS // CORE_COUNT)))
@@ -123,7 +122,7 @@ async def fortunes_test(request):
123122
async with db_pool.acquire() as db_conn:
124123
fortunes = await db_conn.fetch("SELECT * FROM Fortune")
125124

126-
fortunes.append(ADDITIONAL_ROW)
125+
fortunes.append([0, "Additional fortune added at request time."])
127126
fortunes.sort(key=lambda row: row[1])
128127
data = fortune_template.render(fortunes=fortunes)
129128
return bs.html(data)
@@ -173,4 +172,4 @@ def create_fork():
173172
for i in range(1, workers):
174173
create_fork()
175174

176-
run_app()
175+
run_app()

frameworks/Python/emmett55/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ async def pipe(self, next_pipe, **kwargs):
7676

7777
SQL_SELECT = 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1'
7878
SQL_UPDATE = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
79-
ROW_ADD = [0, 'Additional fortune added at request time.']
8079
sort_key = itemgetter(1)
8180

8281

@@ -115,7 +114,7 @@ async def get_random_worlds(db):
115114
@app.route(pipeline=[TemplatePipe("fortunes.html"), db_ext.pipe])
116115
async def fortunes(db):
117116
fortunes = await db.fetch('SELECT * FROM Fortune')
118-
fortunes.append(ROW_ADD)
117+
fortunes.append([0, "Additional fortune added at request time."])
119118
fortunes.sort(key=sort_key)
120119
return {"fortunes": fortunes}
121120

frameworks/Python/fastapi/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
READ_ROW_SQL = 'SELECT "id", "randomnumber" FROM "world" WHERE id = $1'
1919
WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
20-
ADDITIONAL_ROW = [0, "Additional fortune added at request time."]
2120
MAX_POOL_SIZE = 1000//multiprocessing.cpu_count()
2221
MIN_POOL_SIZE = max(int(MAX_POOL_SIZE / 2), 1)
2322

@@ -98,7 +97,7 @@ async def fortunes(request: Request):
9897
async with app.state.connection_pool.acquire() as connection:
9998
fortunes = await connection.fetch("SELECT * FROM Fortune")
10099

101-
fortunes.append(ADDITIONAL_ROW)
100+
fortunes.append([0, "Additional fortune added at request time."])
102101
fortunes.sort(key=lambda row: row[1])
103102
return templates.TemplateResponse("fortune.html", {"fortunes": fortunes, "request": request})
104103

frameworks/Python/fastwsgi/app-asgi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ async def db_setup():
4040

4141
READ_ROW_SQL = 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1'
4242
WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
43-
ADDITIONAL_ROW = [0, 'Additional fortune added at request time.']
4443

4544
JSON_RESPONSE = {
4645
'type': 'http.response.start',
@@ -141,7 +140,7 @@ async def fortunes(scope, receive, send):
141140
finally:
142141
await db_pool.release(db_conn)
143142

144-
fortunes.append(ADDITIONAL_ROW)
143+
fortunes.append([0, "Additional fortune added at request time."])
145144
fortunes.sort(key = _get_item1)
146145
content = fortunes_template.render(fortunes=fortunes)
147146
await send(HTML_RESPONSE)

frameworks/Python/flask/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ def generate_ids(num_queries):
115115
'SELECT world."randomnumber", world."id" FROM "world" WHERE id = $1'
116116
)
117117
write_row_sql = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
118-
ADDITIONAL_ROW = (0, "Additional fortune added at request time.")
119118
db = POOL.getconn()
120119
cursor = db.cursor()
121120
cursor.execute("PREPARE read_stmt (int) AS " + prepared_read_row_sql)
@@ -226,7 +225,7 @@ def get_fortunes_raw():
226225
cursor = db.cursor()
227226
cursor.execute("EXECUTE fortune")
228227
fortunes = list(cursor.fetchall())
229-
fortunes.append(ADDITIONAL_ROW)
228+
fortunes.append((0, "Additional fortune added at request time."))
230229
fortunes.sort(key=itemgetter(1))
231230
POOL.putconn(db)
232231
return flask.Response(FORTUNE_TEMPLATE.render(fortunes=fortunes))

frameworks/Python/granian/app_asgi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ async def pg_setup():
3535

3636
SQL_SELECT = 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1'
3737
SQL_UPDATE = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
38-
ROW_ADD = [0, 'Additional fortune added at request time.']
3938

4039
JSON_RESPONSE = {
4140
'type': 'http.response.start',
@@ -123,7 +122,7 @@ async def route_fortunes(scope, receive, send):
123122
async with pool.acquire() as connection:
124123
fortunes = await connection.fetch('SELECT * FROM Fortune')
125124

126-
fortunes.append(ROW_ADD)
125+
fortunes.append([0, 'Additional fortune added at request time.'])
127126
fortunes.sort(key=key)
128127
content = template.render(fortunes=fortunes).encode('utf-8')
129128
await send(HTML_RESPONSE)

frameworks/Python/granian/app_rsgi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ async def pg_setup():
3535

3636
SQL_SELECT = 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1'
3737
SQL_UPDATE = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
38-
ROW_ADD = [0, 'Additional fortune added at request time.']
3938

4039
JSON_HEADERS = [('content-type', 'application/json')]
4140
HTML_HEADERS = [('content-type', 'text/html; charset=utf-8')]
@@ -102,7 +101,7 @@ async def route_fortunes(scope, proto):
102101
async with pool.acquire() as connection:
103102
fortunes = await connection.fetch('SELECT * FROM Fortune')
104103

105-
fortunes.append(ROW_ADD)
104+
fortunes.append([0, 'Additional fortune added at request time.'])
106105
fortunes.sort(key=key)
107106
content = template.render(fortunes=fortunes)
108107
proto.response_str(

0 commit comments

Comments
 (0)