Skip to content

Instantly share code, notes, and snippets.

@tobami
Created April 15, 2013 14:19
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 tobami/5388430 to your computer and use it in GitHub Desktop.
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
#!/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()
@tobami
Copy link
Author

tobami commented Apr 15, 2013

Just call

curl localhost:5000/10

for a request that blocks for 10 seconds, then

curl localhost:5000/

for a request that does nothing but will be blocked until the first one ends, but which will finish immediately when choosing -a yes

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