This page describes configuration options of wsgi_lineprof. You can provide various options as keyword arguments to change the behavior of the profiler:

from wsgi_lineprof.middleware import LineProfilerMiddleware
app = LineProfilerMiddleware(app, **options)


Users can get results from specific files or sort results by using filters. For example, use FilenameFilter to filter results with filename and use TotalTimeSorter to sort results by total_time.

import time

import bottle
from wsgi_lineprof.filters import FilenameFilter, TotalTimeSorter
from wsgi_lineprof.middleware import LineProfilerMiddleware

app = bottle.default_app()

def get_name():
    # Get some data...
    return "Monty Python"

def index():
    name = get_name()
    return "Hello, {}!!".format(name)

if __name__ == "__main__":
    filters = [
        # Results which filename contains ""
        # Sort by total time of results
    # Add wsgi_lineprof as a WSGI middleware
    app = LineProfilerMiddleware(app, filters=filters)

Run the above script to start web server, then access You can see results in stdout.

$ ./
Bottle v0.12.10 server starting up (using WSGIRefServer())...
Listening on
Hit Ctrl-C to quit.

Time unit: 1e-06 [sec]

File: ./
Name: index
Total time: 1.00526 [sec]
  Line      Hits         Time  Code
    15                         @app.route('/')
    16                         def index():
    17         1      1005250      name = get_name()
    18         1           11      return "Hello, {}!!".format(name)

File: ./
Name: get_name
Total time: 1.00523 [sec]
  Line      Hits         Time  Code
    10                         def get_name():
    11                             # Get some data...
    12         1      1005226      time.sleep(1)
    13         1            4      return "Monty Python" - - [30/Nov/2016 17:21:12] "GET / HTTP/1.1" 200 21

There are more useful filters in wsgi_lineprof.filters. Examples:

  • FilenameFilter("(file1|file2).py", regex=True)

  • NameFilter("(fun1|fun2).py", regex=True)


By using stream option, you can output results to a file. For example, you can output logs to lineprof.log.

f = open("lineprof.log", "w")
app = LineProfilerMiddleware(app, stream=f)

Async Stream

By using async_stream option, wsgi_lineprof starts a new thread for writing results. This option is useful when you do not want the main thread blocked for writing results.

# Start a new thread for writing results
app = LineProfilerMiddleware(app, async_stream=True)

Accumulate Mode

By default, wsgi_lineprof writes results every time a request is processed. By enabling accumulate option, wsgi_lineprof accumulate results of all requests and writes the result on interpreter termination.

app = LineProfilerMiddleware(app, accumulate=True)

Colorize Output

Colorized output is enabled by default for stdout and stderr. You can disable the feature using the color option.

app = LineProfilerMiddleware(app, color=False)

Result Endpoint

By default, you can access an endpoint /wsgi_lineprof/ to see the results. This endpoint is configurable.

app = LineProfilerMiddleware(app, endpoint='/custom_result_endpoint/')