Flask WSGI application hangs when import nltk

2024/10/7 18:28:41

I followed the instructions here to create a onefile flask-app deployed to apache2 with mod-wsgi on ubuntu. That all works fine when using the original flask app. However, when adding import nltk to the flask app apache hangs (no 500).

I use python 2.7 and nltk 2.0.4

Others seem to have had similar problems with other packages. Setting

WSGIApplicationGroup %{GLOBAL}

in the VirtualHost configuration seemed to have helped. However, I still get the same behavior. Did anybody run into the same issue? Thanks for the help!

Here is the VirtualHost Configuration file:

<VirtualHost *:8080># ---- Configure VirtualHost Defaults ----ServerAdmin [email protected] DocumentRoot /home/bitnami/public_html/http<Directory />Options FollowSymLinksAllowOverride None</Directory><Directory /home/bitnami/public_html/http/>Options Indexes FollowSymLinks MultiViewsAllowOverride NoneOrder allow,denyAllow from all</Directory># ---- Configure WSGI Listener(s) ----WSGIDaemonProcess flaskapp user=www-data group=www-data processes=1 threads=5WSGIScriptAlias /flasktest1 /home/bitnami/public_html/wsgi/flasktest1.wsgi <Directory /home/bitnami/public_html/http/flasktest1>WSGIProcessGroup flaskappWSGIApplicationGroup %{GLOBAL}Order deny,allowAllow from all</Directory># ---- Configure Logging ----ErrorLog /home/bitnami/public_html/logs/error.log
LogLevel warn
CustomLog /home/bitnami/public_html/logs/access.log combined

Here is the modified flask code

#!/usr/bin/python
from flask import Flaskimport nltk
app = Flask(__name__)
@app.route('/')
def home():return """<html><h2>Hello from Test Application 1</h2></html>"""@app.route('/<foo>')
def foo(foo):return """<html><h2>Test Application 1</2><h3>/%s</h3></html>""" % fooif __name__ == '__main__':"Are we in the __main__ scope? Start test server."app.run(host='0.0.0.0',port=5000,debug=True)
Answer

Where you have:

<Directory /home/bitnami/public_html/http/flasktest1>WSGIProcessGroup flaskappWSGIApplicationGroup %{GLOBAL}Order deny,allowAllow from all
</Directory>

it should be:

<Directory /home/bitnami/public_html/http>WSGIProcessGroup flaskappWSGIApplicationGroup %{GLOBAL}Order deny,allowAllow from all
</Directory>

as you are neither running your app in daemon mode or in the main interpreter because of the directives being in the wrong context.

That Directory directive then conflicts with one for same directory above so merge them.

If using mod_wsgi 3.0 or later count instead perhaps drop that second Directory block and use:

WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias /flasktest1 /home/bitnami/public_html/wsgi/flasktest1.wsgi process-group=flaskapp application-group=%{GLOBAL}

Note that processes=1 has been dropped as that is the default and setting it implies other things you likely don't want. You also don't need to set user/group as it will automatically run as the Apache user anyway.

https://en.xdnf.cn/q/70212.html

Related Q&A

python append folder name to filenames in all sub folders

I am trying to append the name of a folder to all filenames within that folder. I have to loop through a parent folder that contain sub folders. I have to do this in Python and not a bat file.Example i…

When ruamel.yaml loads @dataclass from string, __post_init__ is not called

Assume I created a @dataclass class Foo, and added a __post_init__ to perform type checking and processing.When I attempt to yaml.load a !Foo object, __post_init__ is not called.from dataclasses import…

How is the python module search path determined on Mac OS X?

When a non built-in module is imported, the interpreter searches in the locations given by sys.path. sys.path is initialized from these locations (http://docs.python.org/library/sys.html#sys.path):the …

Apply Mask Array 2d to 3d

I want to apply a mask of 2 dimensions (an NxM array) to a 3 dimensional array (a KxNxM array). How can I do this?2d = lat x lon 3d = time x lat x lonimport numpy as npa = np.array([[[ 0, 1, 2],[ 3,…

Server Side Google Markers Clustering - Python/Django

After experimenting with client side approach to clustering large numbers of Google markers I decided that it wont be possible for my project (social network with 28,000+ users).Are there any examples …

Tensorflow numpy image reshape [grayscale images]

I am trying to execute the Tensorflow "object_detection_tutorial.py" in jupyter notebook, with my trained neural network data but it throws a ValueError. The file mentioned above is part of S…

Merge numpy arrays returned from loop

I have a loop that generates numpy arrays:for x in range(0, 1000):myArray = myFunction(x)The returned array is always one dimensional. I want to combine all the arrays into one array (also one dimensio…

Play mp3 using Python, PyQt, and Phonon

I been trying all day to figure out the Qts Phonon library with Python. My long term goal is to see if I could get it to play a mms:// stream, but since I cant find an implementation of this done anywh…

Python dictionary keys(which are class objects) comparison with multiple comparer

I am using custom objects as keys in python dictionary. These objects has some default hash and eq methods defined which are being used in default comparison But in some function i need to use a diffe…

How can I make np.save work for an ndarray subclass?

I want to be able to save my array subclass to a npy file, and recover the result later.Something like:>>> class MyArray(np.ndarray): pass >>> data = MyArray(np.arange(10)) >>&g…