[Python] How to profile a FastAPI application running with Hypercorn (handling forked...

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to profile my Python module to find performance bottlenecks.

    The module starts a Hypercorn server, on top of which an asynchronous FastAPI application runs.

    Here’s a rough idea of how I launch it in my main.py:

    hypercorn_config = Config()
    hypercorn_config.bind = "localhost:8000"
    hypercorn_config.workers = 1 #I tried for both 1 and multiple workers
    hypercorn_config.application_path = f"service:create_app({config_str})"
    run(hypercorn_config)


    The create_app() method creates and returns the FastAPI app instance.

    Problem I tried several profilers:


    • yappi


    • scalene


    • cProfile


    • pyinstrument


    • py-spy


    • memray (for memory profiling)

    But I am running into problems:


    1. Hypercorn forks the process, so many tools (like yappi, scalene) can't correctly track the child processes.


    2. pyinstrument can only generate one profile per request, but I need an aggregate profile across multiple requests.


    3. py-spy can attach to the process but suffers from undersampling in async-heavy workloads.


    4. memray also struggles to track memory usage correctly across forked workers.

    How can I properly profile (both CPU and memory) my FastAPI app running under Hypercorn, accounting for the forking behavior? Ideally, I want to capture an end-to-end performance profile over multiple requests. Is there any standard tools, tricks, or best practices to go about it?

    Continue reading...

Compartilhe esta Página