views:

127

answers:

2

Conceptually, I would like to accomplish the following but have had trouble understand how to code it properly in Python:

from threading import Thread
for i in range(0,3):
    t = Thread(target=myfunction)
    t.start()

# wait until threads have finished executing
print 'complete!'
A: 

I have never used python, but I think the concept you are looking for is a "semaphore".

Google turned up this:

http://www.python.org/doc/2.5.2/lib/semaphore-objects.html

EJB
+6  A: 

add the threads to a list and join() them.

from threading import Thread
tlist = []
for i in range(3):
    t = Thread(target=some_function)
    t.start()
    tlist.append(t)

# wait until threads have finished executing
for t in tlist:
    t.join()

print 'complete!'
nosklo
Just be careful that all your threads complete using this code you could potentially end up blocking forever.
Mykroft
@Mykroft: If any threads don't complete code will block anyway.
nosklo