Skip to content

Instantly share code, notes, and snippets.

@madhavan-rp
Last active August 29, 2015 13:56
Show Gist options
  • Save madhavan-rp/9271697 to your computer and use it in GitHub Desktop.
Save madhavan-rp/9271697 to your computer and use it in GitHub Desktop.
Flask Snippets
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")
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