[mod_python] Dummy threads

Graham Dumpleton graham.dumpleton at gmail.com
Sat Sep 15 02:57:29 EDT 2007


On 15/09/2007, Odile Bénassy <odile at ofset.org> wrote:
> On Sat, 15 Sep 2007 09:18:39 +1000
> "Graham Dumpleton" <graham.dumpleton at gmail.com> wrote:
>
> > On 14/09/2007, Odile Bénassy <odile at ofset.org> wrote:
> > > Hello,
> > >
> > > I'm currently trying to port glasnost CMS on Apache2 (*) and encounter
> > > issues related to Dummy Threads
> > >
> > > Here is my test.py file (I use it as a mod python handler):
> > >
> > > --%<
> > > #!/usr/bin/env python
> > >
> > > import threading
> > > from mod_python import apache
> > >
> > > def handler(req):
> > >     thread = threading.currentThread()
> > >     r = req.the_request
> > >     req.content_type = "text/plain"
> > >     req.write("Bonjour!")
> > >     req.write("\n")
> > >     req.write(r)
> > >     req.write("\n")
> > >     req.write(repr(thread))
> > >     return apache.OK
> > >
> > > --%<
> > >
> > > As you can see, it is a rather minimal handler.
> > >
> > > At first it renders:
> > >
> > > --%<
> > > Bonjour!
> > > GET / HTTP/1.1
> > > <_MainThread(MainThread, started)>
> > > --%<
> > >
> > > but if I call any URL more than 3 or 4 times in a row, I get this on the
> > > browser
> > >
> > > -%<
> > > Bonjour!
> > > GET / HTTP/1.1
> > > <_DummyThread(Dummy-1, started daemon)>
> > > --%<
> > >
> > > Can you help me figuring out why this dummy & annoying thing is happening?
> >
> > It is normal.
> >
> > When Apache worker MPM, or Windows MPM are used, then multiple
> > requests are used to handle requests. All these threads are created by
> > Apache and not Python. Which ever Apache thread so happened to be the
> > one which initialised Python will show as MainThread. Any other Apache
> > threads will be maked by Python as a DummyThread, which indicates they
> > are a foreign thread created outside of Python, as is the case in
> > Apache. Because requests are handled in random thread from Apache
> > thread pool, sometimes you will thus see MainThread and other times
> > you will see DummyThread.
> >
> > Why is this creating a problem?
> >
> > Graham
> >
>
> Thank you for this answer
> I will read more about MPM
> It is annoying, because the glasnost CMS uses the thread to pass
> context information from one page to the other, and dummy threads break
> everything

Using the thread object doesn't seem a very safe way of transferring
information around. Normally people would use thread local data
storage mechanisms instead. Either mechanism will not work though as a
way of persisting data between successive requests.

You may find it interesting to read:

  http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

This talks about different Apache MPMs, but also about issues related
to data persistence between requests. This document is for mod_wsgi,
but same applies to mod_python.

Graham



More information about the Mod_python mailing list