[Flask] flask queue question

Ziirish ziirish at ziirish.info
Fri Jul 7 08:10:48 EDT 2017


Hello,

Here is a simplified view of the underlying parts on which the Flask's
"built-in server" is built (with the default settings):

Flask.run() -> werkzeug.serving.run_simple() -> BaseHTTPServer.HTTPServer() -> SocketServer.TCPServer()

Sources:

https://github.com/pallets/flask/blob/master/flask/app.py#L823
https://github.com/pallets/werkzeug/blob/master/werkzeug/serving.py#L605
https://github.com/pallets/werkzeug/blob/master/werkzeug/serving.py#L576
https://docs.python.org/2/library/basehttpserver.html
https://docs.python.org/2/library/socketserver.html#SocketServer.TCPServer

On this last page you can read:

> These four classes process requests synchronously; each request must be
> completed before the next request can be started. This isn’t suitable if
> each request takes a long time to complete, because it requires a lot of
> computation, or because it returns a lot of data which the client is slow
> to process. The solution is to create a separate process or thread to handle
> each request; the ForkingMixIn and ThreadingMixIn mix-in classes can be used
> to support asynchronous behaviour.

So yes, the requests are processed synchronously by default, and the request
queue size is 5 (https://docs.python.org/2/library/socketserver.html#SocketServer.BaseServer.request_queue_size).
This means you can only have 5 queued requests, the other will just get
rejected.

Unfortunately, werkzeug does not provide any mean to override the default
request_queue_size, but according to the socket.listen doc (where this setting
is used), this value seem reasonably chosen: https://docs.python.org/2/library/socket.html#socket.socket.listen


Now, here are your options. As stated in the doc (http://flask.pocoo.org/docs/0.12/deploying/),
the built-in server is *not* suitable for *production*. You can however choose
whatever solution you feel the more comfortable with here:
http://flask.pocoo.org/docs/0.12/deploying/#self-hosted-options
These will probably allow you to tweak the queue size, serve multiple requests
concurrently, etc.



* On Friday, July 07, 2017 at 06:33 AM -0400, Rita <rmorgan466 at gmail.com> wrote:
> yes, i understand that. how come when i submit ten concurrent requests none
> of them get lost and eventually finish? how is that maintained?
> 
> On Fri, Jul 7, 2017 at 3:19 AM, Abdesslem Amri <amriabdesslem at gmail.com>
> wrote:
> 
> > Using the simple app.run() from within Flask creates a single synchronous
> > server on a single thread capable of serving only one client at a time.
> >
> > 2017-07-07 3:02 GMT+02:00 Rita <rmorgan466 at gmail.com>:
> >
> >> Been using flask in my lab for the past few years. It just works. We
> >> recently got some grad students and it seems we are seeing some slowness in
> >> our flask applications running on Windows 2012 with enthought python.
> >>
> >> currently I am running it in a single threaded mode with nginx (
> >> http://flask.pocoo.org/docs/0.12/deploying/wsgi-standalone/#proxy-setups).
> >> My question is: When 2 users hit my flask page, does it get blocked until
> >> the first user finishes his request - it seems that the case. Is there a
> >> way to view the "queue" length? How is this queue maintained, or is it done
> >> by operating system networking stack?


More information about the Flask mailing list