[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 
running
up against a strange problem seen by a few people which hasn't really 
been
worked out yet. One previous post where someone had this problem was:

   http://www.modpython.org/pipermail/mod_python/2005-January/017129.html

for more details.

Anyway, let us know what the following produces.


import threading
import time
from mod_python import apache

def openfiletest(req):
     try:
         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: ")
     openfiletest(req)
     req.write("In child thread: ")
     t = threading.Thread(target=openfiletest, args=(req,) )
     t.start()
     time.sleep(1)
     return apache.OK





More information about the Mod_python mailing list