Created
April 15, 2013 14:19
-
-
Save tobami/5388430 to your computer and use it in GitHub Desktop.
Demonstration of the ability to switchable flask server, from the blocking dev server to the async gevent server.
Instructions to test in the comment below
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
#!/usr/bin/env python | |
import time | |
import argparse | |
import gevent | |
import gevent.monkey | |
from gevent.pywsgi import WSGIServer | |
gevent.monkey.patch_all() | |
from flask import Flask, request, Response, render_template | |
app = Flask(__name__) | |
def call_redis(WAIT): | |
time.sleep(WAIT) | |
@app.route('/') | |
@app.route('/<int:wait>') | |
def click(wait=0): | |
start = time.time() | |
stuff = call_redis(wait) | |
wait = time.time() - start | |
return Response({'took {0} seconds'.format(wait)}) | |
def parse_arguments(): | |
"""Gets console arguments""" | |
parser = argparse.ArgumentParser( | |
description='Flask dev server') | |
parser.add_argument( | |
"-a", "--async", default=False, choices=['yes', 'no'], | |
help="Whether to run the server async" | |
) | |
return vars(parser.parse_args()) | |
if __name__ == '__main__': | |
args = parse_arguments() | |
app.debug = True | |
if args['async'] == 'yes': | |
http_server = WSGIServer(('127.0.0.1', 5000), app) | |
http_server.serve_forever() | |
else: | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just call
for a request that blocks for 10 seconds, then
for a request that does nothing but will be blocked until the first one ends, but which will finish immediately when choosing
-a yes