【发布时间】:2012-07-26 13:30:41
【问题描述】:
我有一些 python 代码可能会导致除以 0,但它在 python (3.2) 解释器中正确运行。但是,如果我尝试使用 mod_wsgi 运行它,它只会挂起而不会出现错误,并且不会提供请求。
解释器中的警告(输出正确):pathwayAnalysis.py:30: RuntimeWarning: divide by zero encountered in double_scalars
有人知道使用 mod_wsgi 运行它的正确方法是什么吗?
代码如下。差异和大小都是长度为 2 的 numpy 浮点数组。difference 中的任何一个浮点数都可能为 0(但不能同时为 0)。在此之前添加difference += 0.0001 可以使其正常运行,但这不是一个好的解决方案,因为输出不准确:
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)
执行以下操作无效:
try:
cond = abs(difference[0] / difference[1]) > (size[0] / size[1])
except RuntimeWarning:
cond = True
# hangs before this point
if cond:
'''as above'''
一些测试代码(使用任一difference 定义):
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]
【问题讨论】:
-
您能否指出您正在使用的 mod_wsgi 版本,并提供一个完整的工作自包含的 WSGI hello world,其中包含失败的代码。这样可以轻松测试而不是猜测是否正确复制了代码的意图。因此,从code.google.com/p/modwsgi/wiki/… 中的示例开始,并将您的代码添加到其中以使其失败。示例需要与 Python 3 兼容,尽管目前不兼容。
-
我正在使用 mod_wsgi-3.3.0。稍后我将使用 hello world 示例进行更新,但如果条件更改为
if (abs(difference[0]) / (abs(difference[1]) + 0.001)) > (size[0] / size[1]):,程序运行良好,这似乎是一个无法解决挂起问题的混乱解决方案。 -
@GrahamDumpleton hello world 示例显示了相同的行为。据我所知,它与 python 3 兼容?
-
您能否在code.google.com/p/modwsgi/wiki/ChangesInVersion0304 中下载最新的mod_wsgi 3.4 源代码,看看新版本是否有所作为?
-
如果你的代码不依赖于numpy怎么办?我会冒险猜测这是一个 numpy 问题,而不是 mod_wsgi。具体来说,如果通过设置 'WSGIApplicationGroup %{GLOBAL}' 来强制使用主解释器会发生什么?有些软件包因不能在子解释器中工作而臭名昭著,有时会被锁定。该指令强制使用主解释器。
标签: python apache warnings mod-wsgi