DEV Community

Discussion on: Profiling Flask application to improve performance

Collapse
 
janmpeterka profile image
Jan Peterka • Edited

I don't undertand where should I use the code
app = ProfilerMiddleware(app)

I'm using app factory pattern.

Can you help? :)

Collapse
 
yellalena profile image
aliona matveeva

you can use it at the place where you initially create your Flask app:

app = Flask(__name__)
app = ProfilerMiddleware(app)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
janmpeterka profile image
Jan Peterka

Hey, this didn't work. But this (inside my create_app function) did:



def create_app(config_name):
    application = Flask(__name__)

   ...

    if application.debug and application.config["PROFILER"]:
        from werkzeug.middleware.profiler import ProfilerMiddleware

        application.config["PROFILE"] = True
        application.wsgi_app = ProfilerMiddleware(
            application.wsgi_app,
            restrictions=[30],
            profile_dir="profiler",
            filename_format="{method}-{path}-{time:.0f}-{elapsed:.0f}ms.prof",
        )

Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
francos3 profile image
francos3

Thanks, I probably had the same problem ("Attribute Record no defined for ProfilerMiddleWare") or something like that, but this def works. I use Ubuntu 20.04 with python 3.8.10