[mod_python] CGI to mod_python -- what's the best way?

Wilson Fletcher mgpl1-NoTinnedHamThanks at zeta.org.au
Thu Apr 12 10:18:28 EST 2001


Wouldn't it be better just to buffer the output and then just use one output to send
the entire buffer ? Isn't that what Grisha was suggesting in the first place ?

I don't understand the problem with redirecting stdout. My understanding of Mod-Python
is that it just keeps the intertpreter running so it reduces start up time. I
understood that each CGI session is still, as always, a separate session that fires up
it's own instance of the code. Is this not true ?

Secondly the idea of just rewriting something that works doesn't seem to be a good idea
to me. I would say rewrite if you're finding you are doing a lot of mainatainence on
existing code however if the code works well and has been tested, deployed etc, it
would be better to interfere with it as little as possible unless you already know that
current requirements will make a rewrite inevitable at some stage in the future
(obviously I'm not just talking about a few lines of python here ).

Wilson

"Gregory (Grisha) Trubetskoy" wrote:

> Victor -
>
> Rather than invent ways to deal with legacy CGI code, I would bite the
> bullet and rewrite the code without the use of "print". There are too many
> subtle gotchas with simulating CGI...
>
> Grisha
>
> On Sun, 8 Apr 2001, Victor Muslin wrote:
>
> >
> > Sorry for a long message, but this requires a bit of explanation. I
> > appreciate your patience in advance.
> >
> > I have a bunch of python legacy code that used to be part of a large
> > CGI-based system. This code simply used print statements to output HTML as
> > follows:
> >
> > def foo():
> >          print 'html1'
> >          print 'html2'
> >
> > Now I want to convert CGI to mod_python, but I would like to re-use the
> > legacy code with as little re-writing as possible (obviously the legacy
> > code is a lot lengthier and more complicated than the example above). I am
> > using the publisher module, which requires my code to return a string
> > containing all of the HTML. So I thought I would be clever and do something
> > like this:
> >
> > import sys, cStringIO
> > def handler(req):
> >          out =  sys.stdout = StringIO()
> >          foo()
> >          return out
> >
> > This works great as long as the second request does not arrive before the
> > first one is done. Otherwise, the output gets screwed up. Since "out" is a
> > local variable, each request has its own instance, but sys.stdout is a
> > global. When the second request arrives, sys.stdout gets reassigned and the
> > rest of the output produced by print statements in the foo() function goes
> > to the new StringIO object. For example, if the second request arrives and
> > gets executed between the two print statements of the first request, then
> > the first request's output could be  'html1\n' and the second request's
> > output could be 'html2\nhtml1\nhtml2\n'.
> >
> > Has anyone dealt with such a situation? Any clever suggestion would be
> > appreciated as I hate to have to go into all the legacy code and change it
> > to something like this:
> >
> > def foo():
> >          out = 'html1\n'
> >          out = out + 'html2\n'
> >          return out
> >
> > def handler(req):
> >          return foo()
> >
> > Thanks in advance.
> > __________________________________________________________________________________
> > Victor Muslin      The power of accurate observation is frequently called
> >                           cynicism by those who don't have it.
> >                                        - George Bernard Shaw
> >
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://www.modpython.org/mailman/listinfo/mod_python




More information about the Mod_python mailing list