Introducing blackfire-python command

By Jérôme Vieilledent, on Oct 19, 2020

Blackfire has been supporting Python since January 2020, and it has been continuously improved since then. Support for Django and Flask have been implemented via the Blackfire SDK for Python.

Today I would like to announce a new step forward in the developer experience by introducing the blackfire-python command.

Make the Python Probe Easier to Use

If you are a Python developer using Django or Flask, you are probably used to manipulating middleware. While this concept helps you stay in control of your application’s behavior, it can become cumbersome. You need to explicitly modify your application settings in order to enable or disable some middleware. This is specifically the case when you want to use low-level tools like Blackfire, which won’t affect your application per se.

In order to simplify and to unify the developer experience, we introduced a new command: blackfire-python. This command does the dirty work for you, by adding the appropriate middleware in the appropriate position. The result is that, when using this command, your web application will automatically detect incoming profile requests and activate the probe when neededwithout impacting regular requests.

To use it, prepend your python commands by blackfire-python:

# With Django development server
blackfire-python python manage.py runserver

# With Django using gunicorn webserver
blackfire-python gunicorn myapp.wsgi

# With Flask development server
export FLASK_APP=hello.py
blackfire-python flask run

Note that, at the time of writing this article, only Django and Flask are supported by blackfire-python.

Django and Flask middleware for Blackfire are now deprecated and will raise a warning if you try to use them.
You may use blackfire.patch_all() instead.

Read more in the documentation for Django and Flask integrations.

In case you’re wondering, yes, this is made possible thanks to Monkey Patching 😉

Running CLI Commands

The blackfire-python command is not only about web requests…

In the past, in order to trigger profiles with Python CLI commands using blackfire run, a site-specific configuration hookblackfire_bootstrap.pth, used to be needed.

This is not needed anymore, as you can now profile CLI scripts by using the blackfire-python run command:

blackfire-python run python my_script.py

blackfire-python run wraps the blackfire run command and ensures that the probe is properly activated.

If you previously installed the blackfire_bootstrap.pth script, you must uninstall it before using blackfire-python.

python -m blackfire uninstall-bootstrap

Want to give it a try?

It is now way easier to get started with Blackfire on Python, so what about giving it a try?

  1. If you don’t have an account yet, create one for free
  2. Benefit from our quick install process
  3. If need be, find more advanced settings for Django and other Python apps
  4. Check out our SDK!

Happy Python Profiling!

Jérôme Vieilledent

As a Developer Advocate, Jérôme is all about spreading the love! His technical/development background enable him to talk as a peer to peers with our developer users. You’ll read his tips and advices on performance management with Blackfire. And he’ll support you as a day-to-day user.