Package: pyflame
Generate flamegraphs for Python code, using Brendan Gregg’s excellentFlameGraphproject to perform the heavy lifting.pyflame can be used to invoke a Python script from the command line and generate a
flamegraph of its execution.  It also provides a panel forDjango Debug Toolbarthat
generates a flamegraph for each request, as well as anIPythonextension that provides
a cell magic to generate flamegraphs for display within aJupyter Notebook.Basic InstallationRunpython-mpip install pyflame.Downloadflamegraph.pland then either:make it available via thePATHenvironment variable, orset up the appropriate configuration as described below to indicate where the script
is located on the filesystem.Command Line UsageInvoke usingpython-mpyflame [<options>] <script> [<args>].The following options are supported:-ppath,--flamegraph-script-pathpath: the path to theflamegraph.plscript
(if not specified, search using thePATHenvironment variable).-aarg_string,--flamegraph-extra-argsarg_string: a string specifying extra
command line arguments to pass when invoking flamegraph.pl.-sinterval,--sample-intervalinterval: the amount of time the sampler thread
will wait between capturing stack traces, in seconds (default: 0.001).-opath,--output-filepath: the location to save the flamegraph. If not
specified, save in the current directory using the name of the Python script with an.svgextension appended.Django Debug Toolbar ConfigurationTo enable, addpyflametoINSTALLED_APPSandpyflame.djdt.panel.FlamegraphPaneltoDEBUG_TOOLBAR_PANELSin the
project’s Django settings module.pyflame uses a similar configuration mechanism to that of Django Debug Toolbar.  To
modify the default configuration, add aPYFLAME_CONFIGsetting to the project’s
Django settings module. This must be a dictionary which may contain any of the following
options:FLAMEGRAPH_SCRIPT_PATHDefault:NoneThe path to theflamegraph.plscript. The default ofNonetells pyflame to search
for flamegraph.pl using thePATHenvironment variable.FLAMEGRAPH_SCRIPT_EXTRA_ARGSDefault:[]A list of extra command line arguments to pass when invoking flamegraph.pl.SAMPLE_INTERVALDefault:0.001,The amount of time the sampler thread will wait between capturing stack traces, in
seconds.Jupyter Notebook ConfigurationTo enable the%%pyflamemagic within an IPython kernel running under Jupyter
Notebook, first load the IPython extension using the%load_extmagic:In [1]: %load_ext pyflameTo load the extension automatically each time the IPython kernel starts, list it in theipython_config.pyfile:c.InteractiveShellApp.extensions = [
    'pyflame'
]There are three other configuration attributes that can be set to configure the
extension:PyFlameMagic.flamegraph_script_pathDefault:NoneThe path to theflamegraph.plscript. The default ofNonetells pyflame to search
for flamegraph.pl using thePATHenvironment variable.PyFlameMagic.flamegraph_script_extra_argsDefault:[]A list of extra command line arguments to pass when invoking flamegraph.pl.PyFlameMagic.default_sample_intervalDefault:0.001The amount of time the sampler thread will wait between capturing stack traces, in
seconds.Licensingpyflame is distributed under the terms of theMIT license.  A copy of the license
text is avaiable in theLICENSEfile.Creditspyflame could not exist without the work of Brendan Gregg and other contributors to
createFlameGraph. pyflame also draws inspiration from two related projects. The
original idea was inspired by Bo Lopker’sdjdt-flamegraphproject (pyflame actually
started out as a fork of djdt-flamegraph, but over time I ended up completely rewriting
it).  The approach of spawning a separate thread to sample stack traces usingsys._current_frames()was drawn from Evan Hempel’spython-flamegraphproject.