[Tutor] Queued threads

Jeremy Jones zanesdad at bellsouth.net
Wed Feb 16 13:50:24 CET 2005


Not to beat a dead horse, but....

Liam Clarke wrote:

>Oops, you probably want to do this then- 
>
>for i in range( 0, 3 ):
>     oThread = Thread( target=mainFunction ).start()
>  
>
Thread.start() looks like it returns None.

#############################
In [23]: from threading import Thread

In [24]: import time

In [25]: def foo():
   ....:     print "doing..."
   ....:     time.sleep(15)
   ....:     print "done...."
   ....:

In [26]: t = Thread(target=foo).start()
doing...

In [27]: print t
None

In [28]: done....


In [28]: print t
None

In [29]: t = Thread(target=foo)

In [30]: t.start()
doing...

In [31]: t.isAlive()
Out[31]: True

In [32]: done....


In [32]: t.isAlive()
Out[32]: False
#############################

So, checking the return of Thread.start() doesn't seem like it would do 
what you think it would do.  You probably want to get the thread object 
and check that directly with isAlive().  Oh, and if you spawn a bunch of 
threads at once and you want to wait until the all complete before doing 
something else, do something like this:

#############################
#create a list to contain the threads
thread_list = []
for i in range(10):
    t = Thread(target=foo)
    print "creating thread", t
    #put each thread in the list
    thread_list.append(t)

#iterate over thread list and start each thread
for t in thread_list:
    print "starting thread", t
    t.start()

#iterate over thread list and wait for each thread
for t in thread_list:
    print "waiting for thread", t
    while 1:
        if not t.isAlive():
            break
        time.sleep(.2)
#############################

It'll give you output something like this:

#############################
creating thread <Thread(Thread-35, initial)>
creating thread <Thread(Thread-36, initial)>
creating thread <Thread(Thread-37, initial)>
creating thread <Thread(Thread-38, initial)>
creating thread <Thread(Thread-39, initial)>
creating thread <Thread(Thread-40, initial)>
creating thread <Thread(Thread-41, initial)>
creating thread <Thread(Thread-42, initial)>
creating thread <Thread(Thread-43, initial)>
creating thread <Thread(Thread-44, initial)>
starting thread <Thread(Thread-35, initial)>
starting thread <Thread(Thread-36, initial)>
starting thread <Thread(Thread-37, initial)>
starting thread <Thread(Thread-38, initial)>
doing...
starting thread <Thread(Thread-39, initial)>
doing...
doing...
starting thread <Thread(Thread-40, initial)>
doing...
starting thread doing...
<Thread(Thread-41, initial)>
doing...
starting thread <Thread(Thread-42, initial)>
starting thread <Thread(Thread-43, initial)>
starting thread <Thread(Thread-44, initial)>
doing...
doing...
doing...
waiting for thread <Thread(Thread-35, started)>
doing...
done....
done....
done....
done....
done....
done....
waiting for thread <Thread(Thread-36, stopped)>
waiting for thread <Thread(Thread-37, stopped)>
waiting for thread done....
done....
done....
done....
<Thread(Thread-38, stopped)>
waiting for thread <Thread(Thread-39, stopped)>
waiting for thread <Thread(Thread-40, stopped)>
waiting for thread <Thread(Thread-41, stopped)>
waiting for thread <Thread(Thread-42, stopped)>
waiting for thread <Thread(Thread-43, stopped)>
waiting for thread <Thread(Thread-44, stopped)>
#################################

But in this situation, I totally agree with Max.  You don't need threads 
for this.  Just use os.system.  You could use one of the popens (or the 
new subprocess module - never used that one myself), but os.system 
blocks until the called program exits.

>    while oThread: 
>             print 'sleeping 3 seconds'
>             time.sleep( 3 )
>
>A if <condition> generally has an implicit else: pass clause as I
>think of it, so it will just keep reiterating if the condition isn't
>met.
>  
>

Jeremy Joens


More information about the Tutor mailing list