0

Is it possible to scale a Flask server?

I have a python server which uses Flask, when it gets a request from my web app, it does something according to the endpoint. The issue is that this something can take some time.

I would like to process multiple requests in parallel. In other words, scale it up.

When I send a request one after the other, it just waits for the first one to complete before attending the other, any way to run those in parallel?

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
Edgar Barber
  • 345
  • 3
  • 13
  • You're not specifying exactly how you're running your flask application. – Ignacio Vergara Kausel Nov 15 '17 at 10:27
  • 2
    I suggest you read the deployment guide: http://flask.pocoo.org/docs/latest/deploying/ – Blender Nov 15 '17 at 10:28
  • *Scalable* means running server on multiple machines. This is not really applicable to web-servers, since no syncing between them are needed. Databases are different story. What you're looking for is called multi-threading support, where one thread can serve one user. More threads = more concurrent connections = more load on server. – Andrejs Cainikovs Nov 15 '17 at 10:29
  • 1
    @AndrejsCainikovs no it doesn't. There are many ways of scaling a web app, multiple machines is just one of them. – Daniel Roseman Nov 15 '17 at 10:29
  • @DanielRoseman Ok, there are definitely different cases. But my gut is saying OP doesn't need scaling his web app (yet?). – Andrejs Cainikovs Nov 15 '17 at 10:31
  • I'm running it locally from my home, I'll soon move it to a EC2. I run with a MongoDB on its side. I need a way to receive more requests and handle them in parallel. I see I can use app.run(threaded=True) but from what I understand it's not for production. Should I use nginx? Thanks – Edgar Barber Nov 15 '17 at 10:35
  • Yes, Nginx FTW. Read [this](http://flask.pocoo.org/docs/0.12/deploying/uwsgi/). – Andrejs Cainikovs Nov 15 '17 at 11:03

1 Answers1

0

I assume you are using the flask builtin 'testing' server, which is really not designed for a production workload.

What you want to do is run your application on a proper web server such as Apache or Nginx that supports execution of python, these will easily handle many simultaneous connections

I would start here: http://flask.pocoo.org/docs/0.12/deploying/

in particular looking at these sections

  • uWSGI
  • FastCGI
  • 1
    Can I use those on windows? I have to run it on windows because I use some windows features. – Edgar Barber Nov 15 '17 at 10:59
  • Yes, [you can](http://nginx.org/en/docs/windows.html). But I highly suggest moving to Linux. – Andrejs Cainikovs Nov 15 '17 at 11:04
  • Thank you a lot, very helpful. So just to make sure I got it, I run the Nginx web server and on that I run the FastCGI for the load balancing, correct? It is supported in windows, right? Where do I set the endpoints? – Edgar Barber Nov 15 '17 at 11:21