Pedro Assunção

Way to wait for all threads to finish in Python

Not sure if it's the best way, but I found this example in an webarchive page and it kind of makes more sense than keeping a list of the threads and cycling all the time checking if they are active or not:

while threading.activeCount()>1:
    sleep(1)

Not sure if this works, but have to try :)

Posted by email from nocivus (posterous)

Related:

  1. Can't wait to try Google chrome For one, the concept of one process per tab and...
  2. Python on my E71. Sweet. Posted via email from nocivus’ ramblings...


Categorised as: Uncategorized


6 Comments

  1. nocivus says:

    Thanks for all your input :)

  2. Exactly. Make the threads clean up after themselves.

  3. nocivus says:

    Well, imagine that you want to continue doing something but, from times to times, check if any threads have finished. Hmm, actually I think I gave myself the solution while thinking about it. It can probably be done by passing a callback function to every thread, right? :D

  4. It would. Why would you want to wait for threads to finish if you have something else to do in the mean time?

  5. Pedro says:

    Will that not block on each call to join? What if you want to do some more processing in the main thread in the meantime?

  6. Keep a list of threads and call t.join() on each of them.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>