I kick off my flask app like this:
#!flask/bin/python
from app import app_instance
from gevent.pywsgi import WSGIServer
#returns and instance of the application - using function to wrap configuration
app = app_instance()
http_server = WSGIServer(('',5000), app)
http_server.serve_forever()
And then when I try to execute this code, the requests call blocks until the original request times out. I'm basically invoking a webservice in the same flask app. What am I misunderstanding about gevent? Wouldn't the thread yield when an i/o event occurred?
@webapp.route("/register", methods=['GET', 'POST'])
def register():
form = RegistrationForm(request.form, csrf_enabled=False)
data = None
if request.method == 'POST' and form.validate():
data= {'email': form.email, 'auth_token': form.password,
'name' : form.name, 'auth_provider' : 'APP'}
r = requests.post('http://localhost:5000', params=data)
print('status' + str(r.status_code))
print(r.json())
return render_template('register.html', form=form)
Improve performance in both Blocking and Non-Blocking web servers. Multitasking is the ability to execute multiple tasks or processes (almost) at the same time. Modern web servers like Flask, Django, and Tornado are all able to handle multiple requests simultaneously.
When multiple requests arrive a WSGI server uses a thread pool to create a new thread which will handle the request and return the response. The scope of this thread would be for the single request. Context switching between threads is handled by the OS.
Flask applications are deployed on a web server, either the built-in server for development and testing or a suitable server (gunicorn, nginx etc.) for production. By default, the server can handle multiple client requests without the programmer having to do anything special in the application.
For reference, the Flask benchmarks on techempower give 25,000 requests per second.
I believe the issue is likely that you forgot to monkey patch. This makes it so that all of the normally blocking calls become non-blocking calls that utilize greenlets. To do this just put this code before you call anything else.
from gevent import monkey; monkey.patch_all()
Go to http://www.gevent.org/intro.html#monkey-patching for more on this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With