diff --git a/frameworks/Python/sanic/app.py b/frameworks/Python/sanic/app.py index 47b374ce9a3..4070503205d 100644 --- a/frameworks/Python/sanic/app.py +++ b/frameworks/Python/sanic/app.py @@ -11,6 +11,8 @@ import sanic from sanic import response +from orjson import dumps + logger = getLogger(__name__) @@ -41,23 +43,26 @@ def get_num_queries(queries): return query_count -connection_pool = None sort_fortunes_key = itemgetter(1) template = load_fortunes_template() -app = sanic.Sanic(name=__name__) +app = sanic.Sanic(name=__name__, dumps=dumps) @app.listener('before_server_start') async def setup_database(app, loop): - global connection_pool - connection_pool = await asyncpg.create_pool( - user=os.getenv('PGUSER', 'benchmarkdbuser'), - password=os.getenv('PGPASS', 'benchmarkdbpass'), - database='hello_world', - host='tfb-database', - port=5432 - ) + app.ctx.pool = await asyncpg.create_pool( + user=os.getenv('PGUSER', 'benchmarkdbuser'), + password=os.getenv('PGPASS', 'benchmarkdbpass'), + database='hello_world', + host='tfb-database', + port=5432 + ) + + +@app.listener('after_server_stop') +async def close_database(app, loop): + app.ctx.pool.close() @app.get('/json') @@ -69,7 +74,7 @@ def json_view(request): async def single_database_query_view(request): row_id = randint(1, 10000) - async with connection_pool.acquire() as connection: + async with request.app.ctx.pool.acquire() as connection: number = await connection.fetchval(READ_ROW_SQL, row_id) return response.json( @@ -84,7 +89,7 @@ async def multiple_database_queries_view(request): row_ids = sample(range(1, 10000), num_queries) worlds = [] - async with connection_pool.acquire() as connection: + async with request.app.ctx.pool.acquire() as connection: statement = await connection.prepare(READ_ROW_SQL) for row_id in row_ids: number = await statement.fetchval(row_id) @@ -100,7 +105,7 @@ async def multiple_database_queries_view(request): @app.get('/fortunes') async def fortunes_view(request): - async with connection_pool.acquire() as connection: + async with request.app.ctx.pool.acquire() as connection: fortunes = await connection.fetch('SELECT * FROM Fortune') fortunes.append(ADDITIONAL_ROW) @@ -112,22 +117,21 @@ async def fortunes_view(request): @app.get('/updates') async def database_updates_view(request): - worlds = [] - updates = set() queries = request.args.get('queries', 1) + num_queries = get_num_queries(queries) + # To avoid deadlock + ids = sorted(sample(range(1, 10000 + 1), num_queries)) + numbers = sorted(sample(range(1, 10000), num_queries)) + updates = list(zip(ids, numbers)) - async with connection_pool.acquire() as connection: - statement = await connection.prepare(READ_ROW_SQL_TO_UPDATE) - - for row_id in sample(range(1, 10000), get_num_queries(queries)): - record = await statement.fetchrow(row_id) - world = dict( - id=record['id'], randomNumber=record['randomnumber'] - ) - world['randomNumber'] = randint(1, 10000) - worlds.append(world) - updates.add((world['id'], world['randomNumber'])) + worlds = [ + {"id": row_id, "randomNumber": number} for row_id, number in updates + ] + async with request.app.ctx.pool.acquire() as connection: + statement = await connection.prepare(READ_ROW_SQL) + for row_id, _ in updates: + await statement.fetchval(row_id) await connection.executemany(WRITE_ROW_SQL, updates) return response.json(worlds, headers=get_headers()) diff --git a/frameworks/Python/sanic/benchmark_config.json b/frameworks/Python/sanic/benchmark_config.json index b1bdd529d60..b3c6799b42b 100644 --- a/frameworks/Python/sanic/benchmark_config.json +++ b/frameworks/Python/sanic/benchmark_config.json @@ -23,8 +23,7 @@ "database_os": "Linux", "display_name": "Sanic", "notes": "", - "versus": "None", - "tags": ["broken"] + "versus": "None" } } ] diff --git a/frameworks/Python/sanic/requirements.txt b/frameworks/Python/sanic/requirements.txt index 5b4738e167d..ff8b4afd8a9 100644 --- a/frameworks/Python/sanic/requirements.txt +++ b/frameworks/Python/sanic/requirements.txt @@ -1,4 +1,5 @@ -asyncpg==0.25.0 +asyncpg==0.29.0 Jinja2==3.1.4 -sanic==22.6.1 -uvloop==0.16.0 +sanic==24.6.0 +uvloop==0.20.0 +orjson==3.10.7 \ No newline at end of file diff --git a/frameworks/Python/sanic/sanic.dockerfile b/frameworks/Python/sanic/sanic.dockerfile index 9619237ed21..d12ebcb391a 100644 --- a/frameworks/Python/sanic/sanic.dockerfile +++ b/frameworks/Python/sanic/sanic.dockerfile @@ -1,8 +1,8 @@ -FROM python:3.8 +FROM python:3.12 ADD ./requirements.txt /sanic/requirements.txt -RUN pip3 install cython==0.29.13 && \ +RUN pip3 install cython==3.0.11 && \ pip3 install -r /sanic/requirements.txt ADD ./ /sanic