checking a thread has started

Josiah Carlson jcarlson at uci.edu
Sat Nov 6 18:30:59 CET 2004


Deepak Sarda <deepak.sarda at gmail.com> wrote:
> 
> Hello everyone.
> 
> I have run into something which I believe is a bug or a shortcoming of
> the threading.Thread module.
> 
> My program spawns 15 threads. For this I've creating a new class with
> threading.Thread as the base class. Then I create objects of this
> class and call their start() methods in a loop.
> 
> The program works fine when run locally in a shell. The problem starts
> when it is served through apache as a cgi program. I just can't get
> all the threads running. The constructor is properly called for each
> object but for some of them  - the run() method never gets called. I
> can get around this by introducing a delay in between calls to the
> start() methods of the fifteen objects (using time.sleep(2)).
> 
> I believe this bottleneck is due to Apache server - it just can't
> spawn so many threads so quickly. It just spawns seven threads and
> that's it.

Is it also possible that your threads are finishing before you get done
spawning them?


> My question is: how do I check whether a call to the start() method
> was successful? If there is a mechanism by which I can get this
> information - I can try restarting/recreating the thread.

lst = [(func, args, kwargs), ...]
i = 0
while i < len(lst):
    count = len(threading.enumerate())
    threading.Thread(target=lst[i][0],
                     args=lst[i][1],
                     kwargs=lst[i][2]).start()
    if len(threading.enumerate()) > count:
        i += 1
    else:
        #You really don't want this to loop as fast as it can.
        #1/100 of a second timeout will keep this from being a
        #processor hog...at least during thread startup.
        time.sleep(.01)


As an aside, are you sure you need 15 threads?  Seems a little over the
top to me.

 - Josiah




More information about the Python-list mailing list