[mod_python] threading module within mod_python

Graham Dumpleton grahamd at dscpl.com.au
Fri Oct 7 06:49:37 EDT 2005


On 07/10/2005, at 8:04 PM, Fabiano Sidler wrote:

> Hi!
>
> I wanted the main thread to be notified about raised exceptions in 
> other
> threads. Thus I wrote a threaded handler in a way similar like this:
>
> --- snip ---
> from mod_python import apache
> from threading import currentThread, Thread, Lock
>
> mainthread = currentThread()
> raiselock = Lock()
> raiselist = []
> def Raise(exc):
>  raiselock.acquire()
>  raiseexc.append(exc)
>  raiselock.release()
> mainthread.Raise = Raise
>
> class ReqThread(Thread):
>  def __init__(self, req):
>   Thread.__init__(self)
>   self.req = req
>  def run(self):
>   # Test raise
>   #raise Exception('run()')
>   mainthread.Raise(Exception('run()'))
>   return
>
> def handler(req):
>  exc = None
>  r = ReqThread(req)
>  r.start(); r.join()
>  raiselock.acquire()
>  if len(raiselist): exc = raiselist.pop
>  raiselock.release()
>  if exc: raise exc
>  return apache.OK
> --- snap ---
>
> However, this doesn't work. If I uncomment the raise in ReqThread.run,
> these exceptions even don't appear in the logs until I stop the httpd,

I'm not really sure what you are trying to achieve here, but due to the
way Apache/Python interact anything output to stdout/stderr is as you
have found generally only flushed and output to the log file when Apache
is shutdown and that possibly only if the process shuts down cleanly.

What you are better off doing is catching any exceptions in the separate
thread, formatting your own exception traceback and details and logging
it directly to the Apache log using the apache.log_error() function
provided with mod_python. This is possibly the only way to get the
information to appear immediately.

> which
> causes following additional errors:
>
> [Fri Oct 07 11:12:45 2005] [warn] child process 9229 still did not 
> exit,
> sending a SIGTERM
> [Fri Oct 07 11:12:45 2005] [warn] child process 9230 still did not 
> exit,
> sending a SIGTERM

This will occur when the child process can't be shutdown cleanly within 
a
specific amount of time. The parent process will give up waiting and
forcibly kill it. Things that can cause this are very long running
requests doing actual work, or errant threads that were started and not
stopped and cleaned up properly.

Don't know if it may be relevant or not, but you might want to look at
the req.register_cleanup() method for registering a function to call 
when
request handling is finishing. You might use this to call a function
which ensures that threads started for that request are shutdown and
cleaned up properly.

> Quite strange, isn't it?
> I'm running Kubuntu and httpd uses mpm_prefork. Is there a chance to 
> get my
> Threads working?

To what end are you trying to raise exceptions in other threads? Just
curious as I can't think of why you would want to do it.

Graham



More information about the Mod_python mailing list