Skip to content

Commit d8cc397

Browse files
Update Gunicorn for Production (#387)
* add gunicorn and configure Dockerfile * refactor: replace list with tuple and os.environ for env variables * fix error
1 parent 04c7ec5 commit d8cc397

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

lessons/231/python-app/Dockerfile

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@ COPY . /app
2424
# CMD ["fastapi", "run", "--workers", "2", "main.py"]
2525

2626
# 4 for the second test to achieve full CPU utilization
27-
CMD ["fastapi", "run", "--workers", "4", "main.py"]
27+
CMD ["gunicorn",
28+
"-w", "4",
29+
"-k", "uvicorn.workers.UvicornWorker",
30+
"--timeout", "60",
31+
"--graceful-timeout", "60",
32+
"--log-level", "error",
33+
"main:app",
34+
"--bind", "0.0.0.0:8000"]

lessons/231/python-app/db.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
POSTGRES_URI = POSTGRES_URI = os.environ["POSTGRES_URI"]
1515
POSTGRES_POOL_SIZE = int(os.environ["POSTGRES_POOL_SIZE"])
16-
MEMCACHED_HOST = os.environ.get["MEMCACHED_HOST"]
17-
MEMCACHED_POOL_SIZE = os.environ.get["MEMCACHED_POOL_SIZE"]
16+
MEMCACHED_HOST = os.environ["MEMCACHED_HOST"]
17+
MEMCACHED_POOL_SIZE = os.environ["MEMCACHED_POOL_SIZE"]
1818

1919

2020
# os.environ.get["MEMCACHED_POOL_SIZE"]

lessons/231/python-app/main.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def health():
4545

4646
@app.get("/api/devices", response_class=ORJSONResponse)
4747
def get_devices():
48-
devices = [
48+
devices = (
4949
{
5050
"id": 1,
5151
"uuid": "9add349c-c35c-4d32-ab0f-53da1ba40a2a",
@@ -70,7 +70,7 @@ def get_devices():
7070
"created_at": "2024-08-28T15:18:21.137Z",
7171
"updated_at": "2024-08-28T15:18:21.137Z",
7272
},
73-
]
73+
)
7474

7575
return devices
7676

@@ -144,7 +144,6 @@ async def create_device(
144144

145145
except Exception:
146146
logger.exception("Unknown error")
147-
print(f"Unexpected error during device creation: {str(e)}")
148147
raise HTTPException(
149148
status_code=500, detail="An unexpected error occurred while creating device"
150149
)

lessons/231/python-app/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dnspython==2.7.0
88
email_validator==2.2.0
99
fastapi==0.115.6
1010
fastapi-cli==0.0.6
11+
gunicorn==23.0.0
1112
h11==0.14.0
1213
httpcore==1.0.7
1314
httptools==0.6.4
@@ -18,6 +19,7 @@ markdown-it-py==3.0.0
1819
MarkupSafe==3.0.2
1920
mdurl==0.1.2
2021
orjson==3.10.12
22+
packaging==24.2
2123
prometheus-fastapi-instrumentator==7.0.0
2224
prometheus_client==0.21.1
2325
psycopg==3.2.3

0 commit comments

Comments
 (0)