9

Recently I'm developing a Flask website/api app to Windows Server and saw some weird issue going on.

When someone make a request to a url, occasionally(like 3-4 out of 10 times) it will hang. All I need to do to make that request come through is going to the command line where I fire up the Flask server, and send an interrupt signal. (I'm on windows, so basically "Ctrl + C"). I've googled for a while, but I believe none of the solutions applies. Here's what I've tried:

  • add threaded=true in app.run() as suggested here didn't work.
  • In This link, Author mentioned that some browser may fire up multiple requests at the same time and make the server confused. This probably doesn't apply here either since I've tried to make request from python's requests library and still see the server hangs.
  • Use Gevent or Tornado to serve app as mentioned in here didn't work either.
  • Tried both on python 3 and python 2.7, had same issue.

I have pretty much same setup on my local machine(win7 64bit) and everything just works fine. (The server that has issue is Windows Server 2012)

I'd love to share some code, but I highly doubt it would be code issue, since it works perfectly fine locally.

Has anyone experienced the same thing before?

Community
  • 1
  • 1
xbb
  • 2,073
  • 1
  • 19
  • 34
  • The server included with Flask is only for development. Use gunicorn or uwsgi to run the server in production. – nathancahill Apr 23 '15 at 21:21
  • @nathancahill I understand that, I'm just still on the debugging stage. And as I mentioned in the thread, I'm on windows so I'm not aware of gunicorn or nginx available on windows... – xbb Apr 23 '15 at 21:32
  • Do you have the issue with the example app here: http://flask.pocoo.org/ – nathancahill Apr 23 '15 at 21:45

2 Answers2

6

After checking with colleague(guess I should've done it earlier..), it seems that the command line tool itself is the issue...(can't believe it). After set it up to run as a Windows Scheduled Task, made a few hundreds requests and experienced no issue at all.

xbb
  • 2,073
  • 1
  • 19
  • 34
0

Everything said here is true - when you put it together the right way ;-)

  1. As stated in the OP, it is probably a case that your browser is making multiple requests. i.e. This happened to me via Chrome, but did not happen via simpler methods like nc (netcat) or curl or wget.
  2. Also as stated in the OP - you need multi threaded flask.
  3. But in the OP - when you added the "threading" option, this doesn't work like you did it if you are running the flask CLI tool.
  4. So in the accepted answer, it worked when you switched to "a windows scheduled task" - probably because you were calling the script directly, rather than through the flask cli program AND the script had threaded=true - which will only apply when you call the script directly (no flask CLI). So This is a correct answer - but for a different reason.
  5. The alternate way to do it - would be to correctly enable threads with the flask CLI. This can be done by simply adding --with-threads to the flash command.

In summary - two correct methods will work:

  • Add threaded=true in app.run() and run your script directly
  • Add --with-threads to the flask CLI program and run that way.
Brad
  • 11,262
  • 8
  • 55
  • 74