Skip to content

Instantly share code, notes, and snippets.

@stickytruth
Last active October 17, 2019 05:08
Show Gist options
  • Select an option

  • Save stickytruth/39c3fa2c88f141a0a26b to your computer and use it in GitHub Desktop.

Select an option

Save stickytruth/39c3fa2c88f141a0a26b to your computer and use it in GitHub Desktop.
kernprof and flask
"""
Flask's reloader causes kernprof to fail.
"""
from flask import Flask, request
import time
app = Flask(__name__)
app.testing = True
@app.route('/')
@profile
def index():
time.sleep(1)
fn = request.environ.get('werkzeug.server.shutdown')
if fn:
fn()
return 'ok'
if __name__ == '__main__':
# Fails
# app.run('127.0.0.1', 5000, use_reloader=True)
# Works
# app.run('127.0.0.1', 5000, use_reloader=False)
# Works
with app.test_client() as c:
c.get('/')
$ kernprof -l -v example.py
Wrote profile results to example.py.lprof
Timer unit: 1e-06 s
Total time: 1.00117 s
File: example.py
Function: index at line 13
Line # Hits Time Per Hit % Time Line Contents
==============================================================
13 @app.route('/')
14 @profile
15 def index():
16 1 1001072 1001072.0 100.0 time.sleep(1)
17 1 97 97.0 0.0 fn = request.environ.get('werkzeug.server.shutdown')
18 1 3 3.0 0.0 if fn:
19 fn()
20 1 2 2.0 0.0 return 'ok'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment