Last active
August 29, 2015 13:56
-
-
Save madhavan-rp/9271697 to your computer and use it in GitHub Desktop.
Flask Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from apscheduler.scheduler import Scheduler | |
from datetime import datetime, timedelta | |
sched = Scheduler() | |
def eat(): | |
print("I'm eating a sandwich") | |
from flask import Flask | |
app = Flask(__name__) | |
app.config['DEBUG'] = True | |
@app.route('/', methods=["GET"]) | |
def index(): | |
return "Hello" | |
if __name__=='__main__': | |
sched.add_date_job(lambda: eat(), datetime.now() + timedelta(seconds=3)) | |
sched.start() | |
app.run("0.0.0.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import g | |
""" | |
Log response time for requests | |
""" | |
@app.before_request | |
def before_request(): | |
import time | |
g.request_start_time = time.time() | |
g.request_time = lambda: "%.5fs" % (time.time() - g.request_start_time) | |
@app.after_request | |
def after_request(resp): | |
print(g.request_time()) # Time taken to process the request | |
return resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment