13

I've been working on a Flask app which handles SMS messages using Twilio, stores them in a database, and provides access to a frontend via JSONP GET requests. I've daemonized it using supervisord, which seems to be working pretty well, but every few days it starts to hang (i.e. all requests pend forever or time out) and I have to restart the process. (I've also tried simply running it with nohup, but same problem.) I was suspicious that sqlite3 was somehow blocking occasionally, but my most recent test was to write a request method which didn't involve database access, and that's timing out too. I'm incredibly puzzled -- hopefully you've seen something similar or know what might be causing this.

The relevant code can be found here, and it's currently running (and stalled, as of this post) on my VPS at mattnichols.net:6288

Thanks!

Update: do you think this could be an issue with Flask's dev server? I'd like to believe that wrapping my app with Tornado (or something similar) could solve the problem, but I've also run other things for much longer without problems using the dev server.

flatpickles
  • 2,198
  • 2
  • 15
  • 21
  • 1
    You might be running out of open file handles, especially if this code is getting a lot of requests. Typical ulimit is 4096. Try increasing that, or - even better - move away from sqlite and use a document db. You don't need relational overhead for storing SMS messages. – Burhan Khalid Nov 11 '12 at 17:48
  • Thanks for your suggestions -- I'm actually getting almost no requests at this point, and I'm pretty sure this issue isn't DB-related. Your point about the non-necessity of relational overhead is spot on though, I'll definitely look into switching over to a document-based DB. – flatpickles Nov 11 '12 at 18:06
  • I did find that for me, the Flask dev server would sometimes be incredibly laggy on certain computers (I think it was because of some issue involving IPv6, although I don't recall anymore). I did have better luck running my Flask app on a CherryPy server (link: http://flask.pocoo.org/snippets/24/) – Michael0x2a Nov 11 '12 at 19:34
  • Fair enough - I'm hoping it's just the dev server. I've started it back up using Tornado and hopefully that'll work. – flatpickles Nov 13 '12 at 01:19
  • I had some problem too with flask hanging/stuck and moved to Tornado. And I still have the same problem... – Thomas Dec 12 '13 at 08:54
  • My observation: (1) Flask app froze and was not processing any more requests. (2) I had access to the console, so pressed CTRL+C (3) It unfroze, and all the queued up requests appeared in a flood. (4) Observed the same occasional lag even when I was using a gevent server, and not the default dev server. I got a time out on the browser. NOTE: I am using a MariaDB database with the app. – Raja Dec 02 '21 at 01:31

1 Answers1

9

For the record, this seems to have been solved by running my app using Tornado instead of the Flask dev server. Wrapping my Flask code into a Tornado server was super easy once I decided to do so: consult http://flask.pocoo.org/docs/deploying/wsgi-standalone/#tornado if you find yourself in my same situation.

pyrho
  • 423
  • 7
  • 13
flatpickles
  • 2,198
  • 2
  • 15
  • 21
  • 2
    Thank you so much for posting this! Tornado seems to be solving the problem for me. Please also link to http://stackoverflow.com/questions/11150343/slow-requests-on-local-flask-server . – gatoatigrado Dec 28 '12 at 01:50
  • My Flask app is not even deployed yet, but was hanging for unknown reasons. Well, I didn't restart my computer since yesterday. In any case problem solved, thanks. – Victor RENÉ Mar 14 '14 at 16:41
  • I had the same issue and seemed to fix it for me also. Thanks –  Oct 19 '14 at 22:05