Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Created July 22, 2019 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocodrips/2d20239d8fa0a3630b0bc6437fc07dee to your computer and use it in GitHub Desktop.
Save cocodrips/2d20239d8fa0a3630b0bc6437fc07dee to your computer and use it in GitHub Desktop.
flaskのrouteデコレータとほかのデコレータの併用
import logging
import time
from functools import wraps
from flask import Flask, Response
app = Flask(__name__)
def stop_watch(func):
@wraps(func)
def wrapper(*args, **kargs):
start = time.time()
result = func(*args, **kargs)
elapsed_time = time.time() - start
print(f"{func.__name__}は{elapsed_time}秒かかりました")
return result
return wrapper
@app.route('/')
@stop_watch
def home():
time.sleep(3)
return Response(status=200)
if __name__ == '__main__':
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
app.run(host='0.0.0.0', port=9001)
@cocodrips
Copy link
Author

INFO:werkzeug: * Running on http://0.0.0.0:9001/ (Press CTRL+C to quit)
INFO:werkzeug:127.0.0.1 - - [22/Jul/2019 13:28:23] "homeは3.003606081008911秒かかりました
GET / HTTP/1.1" 200 -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment