Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python/sanic] Update and fix sanic test #9304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions frameworks/Python/sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import sanic
from sanic import response

from orjson import dumps


logger = getLogger(__name__)

Expand Down Expand Up @@ -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')
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions frameworks/Python/sanic/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"database_os": "Linux",
"display_name": "Sanic",
"notes": "",
"versus": "None",
"tags": ["broken"]
"versus": "None"
}
}
]
Expand Down
7 changes: 4 additions & 3 deletions frameworks/Python/sanic/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions frameworks/Python/sanic/sanic.dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down