除法我有一些Python代碼可由0導致分裂,但它在Python(3.2)解釋器正常運行。但是,如果我嘗試使用mod_wsgi運行它,它會掛起而沒有錯誤,並且不會提供請求。mod_wsgi的掛起RuntimeWarning:由零
在解釋警告(輸出是正確的):pathwayAnalysis.py:30: RuntimeWarning: divide by zero encountered in double_scalars
有誰知道什麼是正確的方法使用mod_wsgi的會跑的?
代碼如下。差異和大小都是長度爲2的numpy浮點數組。浮點數difference
可能爲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你好世界在它失敗的代碼。這種方式可以輕鬆測試而不是猜測是否正確複製了代碼的意圖。因此,請從http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide#WSGI_Application_Script_File中的示例開始,並將代碼添加到該代碼中以使其失敗。儘管目前還沒有,但是需要使Python 3兼容。 – 2012-07-26 14:58:00
我正在使用mod_wsgi-3.3.0。我會稍後用hello世界的例子進行更新,但是如果條件變爲'if(abs(difference [0])/(abs(difference [1])+ 0.001))>(size [0]/size [1]):',這似乎是一個混亂的解決方案,不能解決問題。 – kai 2012-07-26 16:11:16
@GrahamDumpleton hello world示例顯示相同的行爲。據我所知,它與python 3兼容? – kai 2012-07-26 17:25:44