真的很奇怪(mod)_python问题
这个很难解释!
我正在编写一个通过 mod_python 运行的 python 应用程序。 对于每个请求,返回的输出都不同,即使逻辑是“固定的”。
我有两个类,classA
和 classB
。 这样:
class ClassA:
def page(self, req):
req.write("In classA page")
objB = ClassB()
objB.methodB(req)
req.write("End of page")
class ClassB:
def methodB(self, req):
req.write("In methodB")
return None
这是我所拥有的内容的大量剪裁版本。 但我剪下来的东西并没有改变控制流程。 只有一个地方调用MethodB()
。 这是来自 classA
中的 __init__()
。
您会期望得到以下输出:
In classA __init__
In methodB
End of __init__
但是,看似随机地要么获得上述正确的输出,要么:
In classA __init__
In methodB
End of __init__
In methodB
堆栈跟踪显示 methodB
正在从 __init__
第二次调用。 methodB
只能调用一次。 如果第二次调用它,您会期望 __init__ 中的其他逻辑也执行两次。 但是在 methodB
执行之前或之后没有任何内容,并且没有递归。
我通常不会使用 SO 进行调试,但我对此已经摸不着头脑有一段时间了。
版本:2.5.2 r252:60911
提前致谢
编辑 一些线索表明问题可能出在其他地方......上述对代码片段的更改会导致每 250 次左右的点击中出现奇怪的输出 1。 这很奇怪。
打印“In methodB”之前的输出越多,随后打印的错误就越多......平均而言,不是按正比。 它甚至在 Lynx 中也能做到。
我要回到绘图板了。
:(
回答
似乎 mod_python 和 Apache 遇到了婚姻问题。重新启动后,对于一些请求来说一切都很好。然后一切都变得越来越梨形。发布时
/etc/rc.d/init.d/httpd stop
需要很长的时间。而且 RAM 也是我对 Apache 的内部结构不太熟悉,但感觉(感谢 Nadia)线程一直保持活动状态并随机插入请求。 再次感谢
按照 S.Lott 和 Nadia 的建议转向 mod_wsgi
!!
this one is hard to explain!
I am writing a python application to be ran through mod_python. At each request, the returned output differs, even though the logic is 'fixed'.
I have two classes, classA
and classB
. Such that:
class ClassA:
def page(self, req):
req.write("In classA page")
objB = ClassB()
objB.methodB(req)
req.write("End of page")
class ClassB:
def methodB(self, req):
req.write("In methodB")
return None
Which is a heavily snipped version of what I have. But the stuff I have snipped doesn't change the control flow. There is only one place where MethodB()
is called. That is from __init__()
in classA
.
You would expect the following output:
In classA __init__
In methodB
End of __init__
However, seemingly randomly either get the above correct output or:
In classA __init__
In methodB
End of __init__
In methodB
The stacktrace shows that methodB
is being called the second time from __init__
. methodB
should only be called once. If it is called a second time, you would expect that the other logic in __init__
be done twice too. But nothing before or after methodB
executes and there is no recursion.
I wouldn't usually resort to using SO for my debugging, but I have been scratching my head for a while on this.
Version: 2.5.2 r252:60911
thanks in advance
Edit
Some clues that the problem might be elsewhere .... The above changes to the snippet result in the weird output 1 in every 250 or so hits. Which is odd.
The more output prior to printing "In methodB", the more it is printed subsequently incorrectly ... on average, not in direct ratio. It even does it in Lynx.
Im going back to the drawing board.
:(
In response to answer
It seems mod_python and Apache are having marital problems. A restart and things are fine for a few requests. Then it all goes increasingly pear-shaped. When issuing
/etc/rc.d/init.d/httpd stop
It takes a weirdly long amount of time. Also RAM is getting eaten up with requests. I am not that familiar with Apache's internals but it feels like (thanks to Nadia) that threads are staying alive and randomly butting in on requests. Which is plain bonkers.
Moving to mod_wsgi as S.Lott and Nadia suggested
thanks again!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我之前在 mod_python 中见过类似的行为。 通常这是因为 apache 正在运行多个线程,其中一个线程正在运行旧版本的代码。 当您刷新页面时,具有旧代码的线程可能正在为该页面提供服务。 我通常通过停止 apache 然后重新启动来解决这个问题。
自行重新启动并不总是有效。 有时即使这样也不起作用! 这可能听起来很奇怪,但在那些没有任何效果的极少数情况下,我的最后手段是在处理程序的第一行添加一个 raise Exception() 语句,刷新页面,重新启动 apache,然后刷新再次页面。 每次都有效。 必须有更好的解决方案。 但这对我有用。 mod_python 肯定能让人发疯!
我希望这会有所帮助。
I've seen similar behaviour with mod_python before. Usually it is because apache is running multiple threads and one of them is running an older version of the code. When you refresh the page chances are the thread with the older code is serving the page. I usually fix this by stoping apache and then restarting it again
Restart on its own doesn't always work. Sometimes even that doesn't work! That might sound strange but my last resort in those rare cases where nothing is working is to add a
raise Exception()
statement on the first line in the handler, refresh the page, restart apache and then refresh the page again. That works every time. There must be a better solution. But that what worked for me. mod_python can drive one crazy for sure!I hope this might help.
我真的不知道,但构造函数不应该返回任何内容,因此删除
return None
。 即使它们可以返回内容,如果函数本身不返回任何内容,也会自动返回None
。我认为 MethodB 中需要一个 self 参数。
编辑:您能显示更多代码吗? 这工作正常。
I don't really know, but constructors aren't supposed to return anything, so remove the
return None
. Even if they could return stuff,None
is automatically returned if a function doesn't return anything by itself.And I think you need a
self
argument in MethodB.EDIT: Could you show more code? This is working fine.