Why is Flask hanging after importing the pandas lib or several other scientific libs? It still works, when running it locally via SSH, but when visiting the URL the browser loads and loads and nothing happens.
3 Answers
The solution is for Ubuntu with Apache2 server. You have to configure the following file:
/etc/apache2/sites-available/your-flask-app-file.conf
paste the following line below WSGIScriptAlias:
WSGIApplicationGroup %{GLOBAL}

- 4,420
- 8
- 54
- 109
-
-
1@geeky_sh It seems that pandas, numpy, matplotlib etc. are using the simplified threading API for manipulation of the Python GIL and thus will not run correctly within any additional sub interpreters created by Python. That's why it is necessary to force the WSGI application to run within the first interpreter, which the above mentioned solution is doing. – sunwarr10r Dec 12 '16 at 11:39
-
This is curious because I've been running pandas on my Apache2 server for nearly a year but just upgraded to 0.19.1 - and my entire site was brought down for a day because I couldn't figure out the problem. Alas, this was the solution. Many thanks. – elPastor Dec 26 '16 at 07:02
-
My web app hung when trying to perform array algebra and this fixed it – public static void Mar 29 '17 at 15:22
-
After a long and painful exercise, I was able to finally get my app running .The issue is with the pandas 0.19.2 built when the application is getting imported in the .wsgi file
To resolve it Remove your imports from the global level and insert them at the function level
import pandas as pd
....
@app.route('/getFunction', methods=["GET"])
def sample_get_function():
movieData=pd.read_csv('someData.csv')
to
....
@app.route('/getFunction', methods=["GET"])
def sample_get_function():
import pandas as pd
movieData=pd.read_csv('someData.csv')
This is not a very good solution but it is working

- 1,090
- 11
- 11
-
This worked for me too...But now getting error for other imports. Not sure how importing something inside a function. I am on Windows 10 though. – Sid Feb 06 '20 at 08:36
The other solution by @saitam was necessary, but not sufficient in my case.
As before edit:
/etc/apache2/sites-available/<your-flask-app>.conf
Add or update WSGIApplicationGroup
with:
WSGIApplicationGroup %{GLOBAL}
and remove or edit WSGIProcessGroup
such that you have:
WSGIProcessGroup %{GLOBAL}
When this was set to something else, the problem persisted.
Some people advocate setting the number of threads to 1, but this was not necessary for me. The following works fine:
WSGIDaemonProcess <...> processes=2 threads=6
Linux version: Ubuntu 14.04.3 LTS
Pandas version: 0.19.2
Python version: 3.4.3
Apache version: 2.4.7

- 1
- 1

- 8,955
- 3
- 53
- 79