Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask alternatives to achieve true multi-threading?

I had implemented a multi-threaded web server using the Flask micro framework. Basically, my server has a task queue and a thread pool. Hence, it can handle multiple requests. Since Flask is implemented in Python and Python threads are not truly concurrent, my web app is a bit laggy.

Are there are any alternatives to Flask to overcome the issue of multi-threading?

like image 205
user2586432 Avatar asked Aug 30 '13 05:08

user2586432


3 Answers

There is a new package in the trend now which is robust for production also, it is implemented in python and its easy to understand. Please do have a look at it. FastAPI

like image 191
vishnun Avatar answered Sep 29 '22 15:09

vishnun


I came across this question and I was a little disappointed nobody had pointed out how flask (and most python web apps are meant to be deployed). See: http://flask.pocoo.org/docs/deploying/#deployment

My preferred deployment option is the super-simple Tornado which works equally well on Linux and Windows (if I am deploying it alongside existing websites, or even a hybrid deployment as part of an existing site, I usually use IIS Application Request Routing [ARR] as a Reverse Proxy to Tornado). I've also used gevent on both with great success.

Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed. Because it is non-blocking and uses epoll, it can handle thousands of simultaneous standing connections, which means it is ideal for real-time web services. Integrating this service with Flask is straightforward:

So, if your flask application is in yourapplication.py, you might create another called tornado_web.py and use it to serve your application like so:

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from yourapplication import app

http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)
IOLoop.instance().start()

via: http://flask.pocoo.org/docs/deploying/wsgi-standalone/#tornado

like image 25
Aaron Avatar answered Oct 19 '22 12:10

Aaron


This isn't Flask's fault, it is a limitation in the Python interpreter, so any framework that you use will be subject to it.

But there is a great way to avoid this problem. To have true concurrence you can use a pool of processes instead of threads. The multiprocessing module provides an API that is compatible with that of the threading module, but it creates child processes for the workers. I have used this module to create background workers for Flask applications and found to work very well.

like image 8
Miguel Avatar answered Oct 19 '22 13:10

Miguel