checking a thread has started
Deepak Sarda
deepak.sarda at gmail.com
Mon Nov 8 00:23:10 EST 2004
Hi Michael. Thanks for the detailed response..
On 6 Nov 2004 21:26:08 -0700, Michael Fuhr <mfuhr at fuhr.org> wrote:
> You can examine os.environ['GATEWAY_INTERFACE'] to see how your
> script is run: the value should be "CGI/1.1" if you're running
> via CGI and "Python-CGI/1.1" if you're under mod_python. This
> environment variable probably won't be set if you run the script
> from the command line, so use has_key() or trap KeyError to avoid
> exceptions.
>
It give 'CGI/1.1' which means no mod_python
> What do you mean "runs as a local user"? CGI programs usually run
> as whatever user the web server runs as, although it's possible to
> have them run as the script's owner (e.g., by setting up suEXEC).
>
Yes, it's using the suEXEC mechanism. The script is in my home folder
so it get's executed as my uid/gid.
>
> That doesn't make sense given the description of StartServers.
> Besides, as I recall, Apache 1.3.27, which you said you were using,
> doesn't even use threads -- each instance of the web server is run
> in a separate process. Have you tested your hypothesis by altering
> the value of StartServers and checking whether your script behaves
> differently?
I couldn't check this yet as the sys admin is not available. but
something related is there in the mail below.
> > The program _always_ successfully creates and completes execution of
> > exactly seven new threads leaving the remaining eight requested
> > threads in no man's land!
>
> How are you determining which threads are starting and which aren't?
Just seeing if the run() is called - with a print.
>
> > So there must be some relation between apache and the script. Also, as
> > I said earlier, when run through a shell as the same user - the script
> > works perfectly fine.
>
> Something's going on but we lack sufficent evidence to say what.
> We haven't seen your code yet -- could you post the smallest possible
> script that demonstrates the problem?
>
>
The following script always works for threads less than 8. Increase it
beyond that and it almost always fails. I say almost always because
sometimes it manages to run. But it fails almost 98% of the time
(unscientific observation!). The key thing is the os.system() call.
Without that, everything works fine. Also - if I start the threads
just after creating them - then the success rate become 98% (again -
guess).
#!/usr/bin/python2.2
import sys, os, threading
class Worker(threading.Thread):
def __init__(self, index):
self._index = index
threading.Thread.__init__(self)
print "Made object", self._index
sys.stdout.flush()
def run(self):
print "Ran thread with index", self._index
sys.stdout.flush()
os.system("date")
sys.stdout.flush()
Worker.count += 1
if __name__ == '__main__':
sys.stderr = sys.stdout
print "Content-type: text/plain\n"
sys.stdout.flush()
Worker.count = 0
tlist = []
for i in range(10):
t = Worker(i)
#t.start()
tlist.append(t)
for t in tlist:
t.start()
for t in tlist:
t.join()
print "Count:", Worker.count
I'll experiment with the other suggestions offered and get back soon.
--
Online: http://www.antrix.net/journal/
More information about the Python-list
mailing list