views:

203

answers:

2

Hello all, 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!!

+1  A: 

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.

Javier Badia
Yup, those errors don't align with my source so I edited the question and it still stands. It will be one of those Doh! moments.
Aiden Bell
+1 Point for reading. I knew it would likely be fruitless because it is a really odd presentation. :) Thanks!
Aiden Bell
+4  A: 

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.

Nadia Alramli
This seems feasable. The error rate seems to increase from low to high after a restart *scratches chin*
Aiden Bell
Still odd that the inner method gets called 'out of stack' sequence
Aiden Bell
+1 This is what I was thinking. Removed my answer since you said it better and w/ more detail.
Doug
Yea. After a restart things are OK ... then one 'methodB' then 3 then 2 then 5 then 1 again.
Aiden Bell
Also, the faster I refresh, the more random trailing methodBs I get on average
Aiden Bell
@Aiden, believe me I've seen much stranger cases. But I hope my crazy fix will work for you. I don't advice anyone to use mod_python at all!
Nadia Alramli
Can you suggest an alternative?
Aiden Bell
An alternative to mod_python? http://trac.saddi.com/flup I haven't used it but django supports it http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/
Nadia Alramli
mod_wsgi rules. http://code.google.com/p/modwsgi/
S.Lott
Only if the problem is the the publisher and not mod_python itself?
Aiden Bell
@Aiden, I'm having all those issues and I'm not using the publisher
Nadia Alramli
@Aiden, Who said you need to backport to PHP? you have other options than mod_python. Look at face_cgi bindings fore python. Or as S.Lott suggested mod_wsgi.
Nadia Alramli
@Nadia - Thanks :) it's late ... I think ill return to it with a clear head tomorrow. Will install mod_wsgi and give it a go. Dreading PHP without looking at the options :)
Aiden Bell
@Aiden, I hope everything will work out for you :)
Nadia Alramli
@Nadia thank-you :) I never expected to get an [email protected] I'm beginning to think I should have ditched mod_python for wsgi when you mentioned it ages ago. But, new to python and all that
Aiden Bell