【问题标题】:mod_wsgi hangs on RuntimeWarning: divide by zeromod_wsgi 挂起 RuntimeWarning: 除以零
【发布时间】: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


【解决方案1】:

一些使用 C 扩展模块的 Python 第三方包,包括 numpy,只能在 Python 主解释器中工作,不能在子解释器中使用,因为 mod_wsgi 默认使用。结果可能是线程死锁、不正确的行为或进程崩溃。详细内容如下:

http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API

解决方法是强制 WSGI 应用程序在进程的主解释器中运行:

WSGIApplicationGroup %{GLOBAL}

如果在同一台服务器上运行多个 WSGI 应用程序,您可能希望开始使用守护程序模式进行调查,因为某些框架不允许多个实例在同一个解释器中运行。 Django就是这种情况。因此使用守护进程模式,让每个进程都在自己的进程中,并强制每个进程在各自守护进程模式进程组的主解释器中运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多