12

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?

malana
  • 5,045
  • 3
  • 28
  • 41
user2586432
  • 249
  • 1
  • 4
  • 12
  • 1
    In which way are you deploying Flask? It would not be so much on Flask itself. – Joe Doherty Aug 30 '13 at 07:39
  • 1
    Keep in mind that multi-threading in Python could very well be suitable for what you're attempting to do. The "laggy" parts of your view could be things such as web service or database calls, or an improperly set up web server that is creating a Python process for each request. Whatever the actual issue, we don't know what it is to help you solve it with the details you've given. Furthermore, asking for an alternative to Flask is basically asking for a recommendations for a tool, which is not a question that StackOverflow is designed to handle due to the multiple possible answers. – Mark Hildreth Aug 30 '13 at 17:11
  • As you mentioned I am creating a process per request. But I can't avoid it because, web service is exposing a text-to-speech (TTS) system which is implemented as a separate binary. To interact with TTS process I am using pexpect module. So per request, a existing free thread from thread pool will spawn TTS process using pexpect. I know design is bit odd. Here is link to current implementation http://sitspeech.iitkgp.ac.in/. I appreciate any suggestion to improve current design. – user2586432 Aug 31 '13 at 04:43
  • Also what I meant by laggy is "Even if there are tasks in the queue and there are free threads in thread pool, it takes 4-6s before they are handled". I thought it may be because of lot of work ( request, response, swanning TTS process, managing task queue ) done by multiple thread sequentially. Am I wrong with this conclusion – user2586432 Aug 31 '13 at 04:44
  • Same question answered: http://stackoverflow.com/questions/14814201/can-i-serve-multiple-clients-using-just-flask-app-run-as-standalone – ikutsin Feb 09 '14 at 19:15

3 Answers3

25

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

Charles
  • 1,153
  • 1
  • 15
  • 27
Aaron
  • 2,341
  • 21
  • 27
  • I just installed tornado on my app (staright forward as said), but im missing the the helpfull logs of the flask default server with the calls beeing looged on the console in relatime - do you know how this can be done in tornado ? @Aaoron – Jorge Vidinha Aug 20 '16 at 18:48
8

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.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
1

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

vishnun
  • 104
  • 5