1

I am working on a web app using Flask in PyCharm.

I have not had any problem so far, but all of its sudden something changes and it is making the work really difficult.

First thing this the app does is connecting to a network folder and creates a map of all the files and directories in the folder (a network shared folder in a windows computer ). It used to take about five seconds to do that, and it started running the app without any problem.

However, I do not know what happened (I think I did not do anything), but all of its sudden the file mapping takes about three minutes and debugger tries to connect again and start the app again. It does the files mapping again also. After doing this the app start running. But, if I change a line in my code, it does this cycle again.

Since the file mapping became really slow, I checked the computer that hosting the files, but it did not show any sign of problem.

I am really confused because this started happening all of its sudden.

Below is the message I am getting in the console window if PyCharm.

pydev debugger: process 3412 is connecting

Connected to pydev debugger (build 163.15188.4)
 * Restarting with stat
pydev debugger: process 2248 is connecting

 * Debugger is active!
 * Debugger PIN: 288-833-753
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

Did anybody had similar experience? Could you please share your insight of this problem?

Thanks

Kay
  • 1,235
  • 2
  • 13
  • 27
  • try to do this `if __name__ == "__main__": app.run('localhost', 8000, debug=True)` – P.hunter Feb 18 '18 at 08:55
  • anything special about port 8000? other than the port number, the line is the same. I am using the port 80 to make it easier for my testers to use. Should I not use port 80? – Kay Feb 18 '18 at 09:01
  • it doesn't matter for testing but if ant other port running for http service ,as a practice you can go for it or 8080 – P.hunter Feb 18 '18 at 09:05
  • I changed port to 8000 and 8080, it but did not make difference. – Kay Feb 18 '18 at 09:08
  • actually the problem is not with the port as we see, the debugger is running twice, have you set any break point manually in it ? – P.hunter Feb 18 '18 at 09:11
  • I tried to debug the app, and that is how I found out that it is actually trying to start the app twice. It actually hits the break point of the app twice in the in the initialization code. – Kay Feb 18 '18 at 09:21
  • you don't need to set the break point manually if you are doing `debug=True` so you just have to remove the break point, if it still doesn't work try running the script from command line check if takes the same time. – P.hunter Feb 18 '18 at 09:36

1 Answers1

1

I would try running with debug=False or use_reloader to prevent flask from restarting after detecting a change in your files and see if anything changes.

More info about the reloaded here: How to stop Flask from initialising twice in Debug Mode?

The slowness in the startup could be attributed to any long running tasks in your app initialization code.

You would need to include more code or profile the code yourself to troubleshoot that.

If you are reading or writing from/to a network location IO might be your bottleneck.

Lastly, I recently went through an effort of troubleshooting/optimizing a flask app startup time - maybe something there could be useful to you: Slow Flask Development Server Initialization Profiling: `WaitForSingleObject`

gtalarico
  • 4,409
  • 1
  • 20
  • 42
  • thank you very much for the suggestion, I will try that. However, do you have any suggestion for debugger trying to connect twice in the beginning of executing the the app? – Kay Feb 18 '18 at 09:14
  • In debug mode, flask starts twice, one main thread + another thread to watch system changes and reload app when needed. This is normal and useful during development. You should be able to prevent the double initialization by disabling debug mode or disabling the reloader – gtalarico Feb 18 '18 at 09:30
  • thank you so much for your help. Now I blocked the re-initialization. I guess the real problem that I need to solve is why it is taking too long all of its sudden. I tried reboot the files hosting machine and I do not see any performance problem at all. It is so strange. My comcast network speed test also shows no problem. It is really driving me crazy. – Kay Feb 18 '18 at 09:37
  • The body of your flask app should be as minimal as possible to keep the boot time fast. I would suggest any expensive startup tasks in something like the flask before_first_request decorator. If your app body is only function/route registrations it should not take more than a few seconds – gtalarico Feb 18 '18 at 09:47