Reputation: 2175
I have some python code that may result in a division by 0, but it runs correctly in a python (3.2) interpreter. However, if I try to run it using mod_wsgi, it simply hangs without an error and the request is not served.
Warning in interpreter (output is correct): pathwayAnalysis.py:30: RuntimeWarning: divide by zero encountered in double_scalars
Does anybody know what the correct way to run this using mod_wsgi would be?
The code is below. Both difference and size are numpy float arrays of length 2. Either float in difference
may be 0 (but not both). Adding difference += 0.0001
before this makes it run correctly, but is not a nice solution since the output is not accurate:
if abs(difference[0] / difference[1]) > (size[0] / size[1]):
ratio = abs(size[0] / difference[0])
else: ratio = abs(size[1] / difference[1])
for i in range(len(base)):
result.append(base[i] + difference[i] * ratio/2)
return array(result)
Doing the following does not work:
try:
cond = abs(difference[0] / difference[1]) > (size[0] / size[1])
except RuntimeWarning:
cond = True
# hangs before this point
if cond:
'''as above'''
Some test code (using either one of the difference
definitions):
def application(environ, start_response):
from numpy import array
size = array([10., 10.])
difference = array([115., 0.]) # hangs
difference = array([115., 10.]) # returns page with text 'Yes.'
if abs(difference[0]/difference[1]) > (size[0]/size[1]):
output = 'Yes.'
else:
output = 'No.'
status = '200 OK'
response_headers = [('Content-type', 'text/plain'),\
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Upvotes: 0
Views: 822
Reputation: 58533
Some third party packages for Python which use C extension modules, and this includes numpy, will only work in the Python main interpreter and cannot be used in sub interpreters as mod_wsgi by default uses. The result can be thread deadlock, incorrect behaviour or processes crashes. These is detailed in:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
The workaround is to force the WSGI application to run in the main interpreter of the process using:
WSGIApplicationGroup %{GLOBAL}
If running multiple WSGI applications on same server, you would want to start investigating using daemon mode because some frameworks don't allow multiple instances to run in same interpreter. This is the case with Django. Thus use daemon mode so each is in its own process and force each to run in main interpreter of their respective daemon mode process groups.
Upvotes: 1