
July 8th, 2004, 04:13 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
At the moment your program does not require threads at all. In the for loop it starts a thread then waits for that thread to finish (the join) before starting the next one.
IMO what your require is the use of events.
The principle is that each thread is started (you can use the for loop but drop the join )
Then the main program busy waits in a while loop monitoring the events- at this point the main thread and child threads are all running in that time sliced multi-tasking mode. The main thread will wait for each threads event before deciding it has finished.
Code:
import threading, time
class Threads(threading.Thread):
def __init__(self, data, signal):
self.data = data
self.signal = signal
threading.Thread.__init__(self)
self.setDaemon(1)
def run(self):
time.sleep(1)
print "Text -> %s"%(self.data)
self.signal.set()
listd = ["sup jo", "Do i know you?"]
slaves = []
for x in listd:
sig = threading.Event()
sig.clear() #make sure the event is not set yet
test = Threads(x, sig)
test.start()
slaves.append((test, sig)) # Keep a copy of the thread and event
print "Waiting"
while len(slaves):
print ".",
for n in slaves: # check each slave
if not n[1].isSet(): # test to see if the thread has triggered it's event
continue # at least one thread is still running so we continue monitoring
n[0].join() #just to be sure we have tidied up
slaves.pop(0) # Because I use continue we only get here on index 0
print "A slave is dead"
print 'Done!!'
By the way do you really need the setDaemon in this situation?
grim 
|