-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Lesson 239: Use multiprocess.MultiProcessCollector from prometheus_client #412
Comments
It’s better to use async handlers because sync handlers always run in a thread pool (which has 40 threads in AnyIO, if I’m not mistaken). However, since Python has a GIL and can run only one thread at a time, this creates unnecessary overhead. @app.get("/healthz", response_class=PlainTextResponse)
async def health():
return "OK"
@app.get("/api/devices", response_class=ORJSONResponse)
async def get_devices(): |
It’s not a good practice to do it like this: async def get_db() -> AsyncGenerator[asyncpg.Connection, None]:
async with db.get_connection() as conn:
yield conn
@app.post("/api/devices", status_code=201, response_class=ORJSONResponse)
async def create_device(
device: DeviceRequest, conn: PostgresDep, cache_client: MemcachedDep
):
...
await conn...
await cache_client...
... Because A better approach is: @app.post("/api/devices", status_code=201, response_class=ORJSONResponse)
async def create_device(
device: DeviceRequest, db: PostgresDep, cache_client: MemcachedDep
):
...
async with db.get_connection() as conn:
await conn...
await cache_client...
... This way, the connection is properly returned to the pool before executing the cache operation, improving resource management. |
I think Python is not faster than Go or Node.js, and that’s okay. But these changes will slightly improve performance and help manage expectations so people won’t be too disappointed. |
Since there are four workers (
"gunicorn", "-w", "4"
), meaning four separate processes, I recommend using multiprocess.MultiProcessCollector from prometheus_client. Otherwise, you’ll collect metrics from only one random process.tutorials/lessons/239/fastapi-app/main.py
Line 21 in feab6d6
https://prometheus.github.io/client_python/exporting/http/fastapi-gunicorn/
The text was updated successfully, but these errors were encountered: