[mod_python] mod_python and threading.Thread doesn't start

Graham Dumpleton grahamd at dscpl.com.au
Mon Jun 6 07:02:49 EDT 2005

On 06/06/2005, at 5:39 PM, Alexey Melchakov wrote:

>  Hi.
>  I'm using mod_python 3.1.4, apache 2.0.x/prefork and python2.4.
>  The following code doesn't work:
>  from mod_python import apache
>  import threading
>  def something():
>      open('/tmp/thread', 'w').close()
>  def handler(req):
>      threading.Thread(target=something).start()
>      return apache.OK
> Browser request successfully hits the handler(), and no errors occure, 
> but something() is not called (no file on filesystem after request).
>  If I insert logging statements after thread initialization and a call 
> of start() thread object looks like this:
>  <Thread(Thread-1, initial)>
>  <Thread(Thread-1, started)>
>  I'm not good with threading and this might be very ordinary 
> situation, dont really know. Please help.

Try the following code instead so that you can see if a Python 
exception is
being raised within the thread. One possibility is that you might be 
up against a strange problem seen by a few people which hasn't really 
worked out yet. One previous post where someone had this problem was:


for more details.

Anyway, let us know what the following produces.

import threading
import time
from mod_python import apache

def openfiletest(req):
         a = file("/tmp/test", "w")
         req.write("file() was OK\n")
     except Exception, e:
         req.write("file() threw exception: " + str(e) + "\n")

def handler(req):
     req.write("In main thread: ")
     req.write("In child thread: ")
     t = threading.Thread(target=openfiletest, args=(req,) )
     return apache.OK

More information about the Mod_python mailing list