Skip to content

Commit 971fcbe

Browse files
Micael Maltafranz1981
Micael Malta
authored andcommitted
[FASTAPI] Bump fastapi to 0.93.0 (TechEmpower#7991)
Introduce lifespan events
1 parent d151bd1 commit 971fcbe

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

frameworks/Python/fastapi/app.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import multiprocessing
2+
from contextlib import asynccontextmanager
3+
14
import asyncpg
25
import os
36
from fastapi import FastAPI, Request
@@ -15,6 +18,8 @@
1518
READ_ROW_SQL = 'SELECT "id", "randomnumber" FROM "world" WHERE id = $1'
1619
WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
1720
ADDITIONAL_ROW = [0, "Additional fortune added at request time."]
21+
MAX_POOL_SIZE = 1000//multiprocessing.cpu_count()
22+
MIN_POOL_SIZE = max(int(MAX_POOL_SIZE / 2), 1)
1823

1924

2025
def get_num_queries(queries):
@@ -34,8 +39,6 @@ def get_num_queries(queries):
3439

3540
templates = Jinja2Templates(directory="templates")
3641

37-
app = FastAPI()
38-
3942

4043
async def setup_database():
4144
return await asyncpg.create_pool(
@@ -44,17 +47,21 @@ async def setup_database():
4447
database="hello_world",
4548
host="tfb-database",
4649
port=5432,
50+
min_size=MIN_POOL_SIZE,
51+
max_size=MAX_POOL_SIZE,
4752
)
4853

4954

50-
@app.on_event("startup")
51-
async def startup_event():
55+
@asynccontextmanager
56+
async def lifespan(app: FastAPI):
57+
# Setup the database connection pool
5258
app.state.connection_pool = await setup_database()
59+
yield
60+
# Close the database connection pool
61+
await app.state.connection_pool.close()
5362

5463

55-
@app.on_event("shutdown")
56-
async def shutdown_event():
57-
await app.state.connection_pool.close()
64+
app = FastAPI(lifespan=lifespan)
5865

5966

6067
@app.get("/json")

frameworks/Python/fastapi/app_orm.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import multiprocessing
33
import os
4+
from contextlib import asynccontextmanager
45
from operator import attrgetter
56
from random import randint, sample
67

@@ -42,6 +43,7 @@ class Fortune(Base):
4243
ADDITIONAL_FORTUNE = Fortune(
4344
id=0, message="Additional fortune added at request time."
4445
)
46+
MAX_POOL_SIZE = 1000//multiprocessing.cpu_count()
4547

4648
sort_fortunes_key = attrgetter("message")
4749

@@ -50,8 +52,6 @@ class Fortune(Base):
5052
)
5153
templates = Jinja2Templates(directory=template_path)
5254

53-
app = FastAPI()
54-
5555

5656
async def setup_database():
5757
dsn = "postgresql+asyncpg://%s:%s@tfb-database:5432/hello_world" % (
@@ -62,13 +62,26 @@ async def setup_database():
6262
engine = create_async_engine(
6363
dsn,
6464
future=True,
65+
pool_size=MAX_POOL_SIZE,
6566
connect_args={
6667
"ssl": False # NEEDED FOR NGINX-UNIT OTHERWISE IT FAILS
6768
},
6869
)
6970
return sessionmaker(engine, class_=AsyncSession)
7071

7172

73+
@asynccontextmanager
74+
async def lifespan(app: FastAPI):
75+
# Setup the database connection pool
76+
app.state.db_session = await setup_database()
77+
yield
78+
# Close the database connection pool
79+
await app.state.db_session.close()
80+
81+
82+
app = FastAPI(lifespan=lifespan)
83+
84+
7285
def get_num_queries(queries):
7386
try:
7487
query_count = int(queries)
@@ -82,16 +95,6 @@ def get_num_queries(queries):
8295
return query_count
8396

8497

85-
@app.on_event("startup")
86-
async def startup_event():
87-
app.state.db_session = await setup_database()
88-
89-
90-
@app.on_event("shutdown")
91-
async def shutdown_event():
92-
await app.state.db_session.close()
93-
94-
9598
@app.get("/json")
9699
async def json_serialization():
97100
return UJSONResponse({"message": "Hello, world!"})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
asyncpg==0.27.0
2-
fastapi==0.92.0
2+
fastapi==0.93.0
33
Jinja2==3.1.2
44
ujson==5.7.0

0 commit comments

Comments
 (0)