2

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

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
  • Did you intend to leave out the "self" on MethodB? – Paul Fisher May 29 '09 at 19:40
  • 2
    Please use Upper Case for Class Names and lower case for method names. And please post a snippet that shows the problem. The stuff you've posted works as expected. Can't help much when it works. – S.Lott May 29 '09 at 19:44
  • @S.Lott - I agree it should work. Other than replacing 'print' with 'req.write()' and a bunch of irrelevant list assignments. That is pretty much it. – Aiden Bell May 29 '09 at 19:47
  • @Aiden: I'm not denying you have a problem. The snippet actually works. So, you'll have to manufacture a snippet that actually fails for us to provide any useful advice. – S.Lott May 29 '09 at 19:50

2 Answers2

4

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
  • 111,714
  • 37
  • 173
  • 152
  • This seems feasable. The error rate seems to increase from low to high after a restart *scratches chin* – Aiden Bell May 29 '09 at 20:19
  • Still odd that the inner method gets called 'out of stack' sequence – Aiden Bell May 29 '09 at 20:19
  • +1 This is what I was thinking. Removed my answer since you said it better and w/ more detail. – Doug May 29 '09 at 20:19
  • 1
    Yea. After a restart things are OK ... then one 'methodB' then 3 then 2 then 5 then 1 again. – Aiden Bell May 29 '09 at 20:22
  • Also, the faster I refresh, the more random trailing methodBs I get on average – Aiden Bell May 29 '09 at 20:23
  • 2
    @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 May 29 '09 at 20:24
  • Can you suggest an alternative? – Aiden Bell May 29 '09 at 20:24
  • 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 May 29 '09 at 20:32
  • Only if the problem is the the publisher and not mod_python itself? – Aiden Bell May 29 '09 at 20:50
  • @Aiden, I'm having all those issues and I'm not using the publisher – Nadia Alramli May 29 '09 at 20:51
  • @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 May 29 '09 at 20:58
  • 2
    @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 May 29 '09 at 21:01
  • 1
    @Nadia thank-you :) I never expected to get an answer! @S.Lott 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 May 29 '09 at 21:10
1

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
  • 4,552
  • 7
  • 36
  • 46
  • 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 May 29 '09 at 19:43
  • +1 Point for reading. I knew it would likely be fruitless because it is a really odd presentation. :) Thanks! – Aiden Bell May 29 '09 at 20:09