Question

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!!

Was it helpful?

Solution

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

sudo /etc/init.d/apache stop
sudo /etc/init.d/apache restart

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top