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

Multiprocessing with Gunicorn + FastAPI #810

Closed
IWillPull opened this issue May 17, 2022 · 5 comments
Closed

Multiprocessing with Gunicorn + FastAPI #810

IWillPull opened this issue May 17, 2022 · 5 comments

Comments

@IWillPull
Copy link

I've been trying to make multiprocessing work with FastAPI, but it just doesn't seem to happen.

This part is what I've been struggling the most:

def app(environ, start_response):
    registry = CollectorRegistry()
    multiprocess.MultiProcessCollector(registry)
    data = generate_latest(registry)
    status = '200 OK'
    response_headers = [
        ('Content-type', CONTENT_TYPE_LATEST),
        ('Content-Length', str(len(data)))
    ]
    start_response(status, response_headers)
    return iter([data])

Had no idea where to put this, but after some experimentation it turns out, the simplest solution of creating a metrics app looks like this (including other parts from the documentation):

app = FastAPI(debug=False)

# Preparing gunicorn multiprocessing HACKS
def make_metrics_app():
    registry = CollectorRegistry()
    multiprocess.MultiProcessCollector(registry)
    return prometheus_client.make_asgi_app(registry=registry)


metrics_app = make_metrics_app()
app.mount("/metrics", metrics_app))

Just posting this to help others save hours of researching and debugging.

@csmarchbanks
Copy link
Member

Thanks for the report! Would you be interested in adding some documentation to the README similar to how we have an example for flask, but under the multiprocess section?

@IWillPull
Copy link
Author

@csmarchbanks If you think this is a correct way of doing things, I will add the instructions to the README

ashirviskas added a commit to ashirviskas/client_python that referenced this issue May 18, 2022
Also provided additional multiprocessing instructions for FastAPI + Gunicorn setup with code examples as per this issue: prometheus#810
@ashirviskas
Copy link
Contributor

ashirviskas commented May 18, 2022

(me from my alt acc)
Added my PR here: #812

ashirviskas added a commit to ashirviskas/client_python that referenced this issue May 18, 2022
Also provided additional multiprocessing instructions for FastAPI + Gunicorn setup with code examples as per this issue: prometheus#810

Signed-off-by: Matas Minelga <minematas@gmail.com.com>
@Canas
Copy link

Canas commented Jun 7, 2022

Looking at the README I see this

It is a best practice to create this registry inside the context of a request

Doesn't this mean that we could add that setup in a route? (see below)
I've seen other people also use the gunicorn when_ready hook to do the setup. Not sure what would be the best way and why.

@app.get("/metrics")
def metrics(request: Request):
    registry = CollectorRegistry()
    multiprocess.MultiProcessCollector(registry)
    data = generate_latest(registry)
    res = Response(content=data)
    res.headers["Content-Type"] = CONTENT_TYPE_LATEST
    return res

ashirviskas added a commit to ashirviskas/client_python that referenced this issue Apr 18, 2023
Also provided additional multiprocessing instructions for FastAPI + Gunicorn setup with code examples as per this issue: prometheus#810

Signed-off-by: Matas Minelga minematas@gmail.com
ashirviskas added a commit to ashirviskas/client_python that referenced this issue Apr 18, 2023
Also provided additional multiprocessing instructions for FastAPI + Gunicorn setup with code examples as per this issue: prometheus#810

Signed-off-by: Matas Minelga <minematas@gmail.com>
csmarchbanks pushed a commit that referenced this issue Apr 18, 2023
Also provided additional multiprocessing instructions for FastAPI + Gunicorn setup with code examples as per this issue: #810

Signed-off-by: Matas Minelga <minematas@gmail.com>
@csmarchbanks
Copy link
Member

Closing this as the information is now in the readme!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants