File: flask_hello.py

package info (click to toggle)
python-pyinstrument 5.1.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,620 kB
  • sloc: python: 6,713; ansic: 897; makefile: 46; sh: 20; javascript: 18
file content (47 lines) | stat: -rw-r--r-- 889 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import time

from pyinstrument import Profiler

try:
    from flask import Flask, g, make_response, request
except ImportError:
    print("This example requires Flask.")
    print("Install using `pip install flask`.")
    exit(1)

app = Flask(__name__)


@app.before_request
def before_request():
    if "profile" in request.args:
        g.profiler = Profiler()
        g.profiler.start()


@app.after_request
def after_request(response):
    if not hasattr(g, "profiler"):
        return response
    g.profiler.stop()
    output_html = g.profiler.output_html()
    return make_response(output_html)


@app.route("/")
def hello_world():
    return "Hello, World!"


@app.route("/sleep")
def sleep():
    time.sleep(0.1)
    return "Good morning!"


@app.route("/dosomething")
def do_something():
    import requests

    requests.get("http://google.com")
    return "Google says hello!"