Using profiling to find bottlenecks

Profiling is an important tool when we decide to scale the application. Before scaling, we want to know whether any process is a bottleneck and affects the overall performance. Python has an inbuilt profiler, cProfile, that can do the job for us, but to make life easier, Werkzeug has a ProfilerMiddleware of its own, which is written over cProfile. We will use this to determine whether there is anything that affects the performance.

Getting ready

We will use the application from the previous recipe and add ProfilerMiddleware in a new file named generate_profile.py.

How to do it…

Create a new file, generate_profile.py, alongside run.py, which works like run.py itself but with ProfilerMiddleware:

from werkzeug.contrib.profiler ...

Get Flask Framework Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.