Jacob Beauchamp
Jacob Beauchamp

Reputation: 540

How to set maximum number of pending connections in Flask

How can I set a maximum number of pending connections in a Flask application?

E.g.

After I run this code, I can send it two requests at the same time. While the first request is being processed, the other one will wait. When the first one is done, the second one will be processed.

from flask import Flask
application = Flask(__name__)


@application.route("/")
def hello():
    for x in range(10000000):
        x += 1
    return "Hello World!"


if __name__ == '__main__':
    application.run()

How can I make it so that when I send two requests at the same time, the first one will be processed and the second one, instead of waiting, will not be able to connected (maybe it will get a some kind of error instead).

Upvotes: 1

Views: 2020

Answers (1)

Saad
Saad

Reputation: 944

You can use Flask with some sort of web server, such as Gunicorn, Nginx or Apache, to accept HTTP requests which it will then operate on. The reason why people run Nginx and Gunicorn together is that in addition to being a web server, Nginx can also proxy connections to Gunicorn which brings certain performance benefits.

Gunicorn is pre-forking software. For low latency communications, such as load balancer to app server or communications between services, pre-fork systems can be very successful. A gunicorn server can; Runs any WSGI Python web application (and framework)

Can be used as a drop-in replacement for Paster (Pyramid), Django's Development Server, web2py etc.

Comes with various worker types and configurations

Manages worker processes automatically

HTTP/1.0 and HTTP/1.1 (Keep-Alive) support through synchronous and asynchronous workers

You can take help from this blogpost, for setting up a flask application with gunicorn.

Upvotes: 1

Related Questions