fastapi_profiler: Runtime Error: This profiler is not currently running

This is my first time using a profiler, how do i make the profiler run? This is how i add the middleware

if app_config.PROFILE_MODE:
    print("__________________profile mode__________________")

    from fastapi_profiler import PyInstrumentProfilerMiddleware

    app.add_middleware(
        PyInstrumentProfilerMiddleware,
        server_app=app,  # Required to output the profile on server shutdown
        profiler_output_type="html",
        is_print_each_request=False,  # Set to True to show request profile on
        # stdout on each request
        open_in_browser=True,  # Set to true to open your web-browser automatically
        # when the server shuts down
        html_file_name="example_profile.html",  # Filename for output)
    )

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 22 (9 by maintainers)

Most upvoted comments

@nhhviet98

  • I found the problem. The main reason is that pyinstrument is thread-unsafe in getting stack information in concurrent scenarios, so sequential calls don’t cause problems, concurrent calls do.
  • Next I tried to see how to fix the problem.

set async_mode="strict" https://pyinstrument.readthedocs.io/en/latest/reference.html#pyinstrument.Profiler.async_mode

app.add_middleware(
    PyInstrumentProfilerMiddleware,
    server_app=app,  # Required to output the profile on server shutdown
    profiler_output_type="html",
    is_print_each_request=False,  # Set to True to show request profile on
                                  # stdout on each request
    open_in_browser=False,  # Set to true to open your web-browser automatically
                            # when the server shuts down
    async_mode="strict",
    html_file_name="example_profile.html"  # Filename for output
)