0

I have a python app that goes like this:

  1. main thread is GUI
  2. there is a config thread that is started even before GUI
  3. config thread starts a few others independent threads

=> How can I let GUI know that all of those "independent threads" (3.) have finished? How do I detect it in my program (just give me general idea)

I know about Semaphores but I couldnt figure it out, since this is a little more logically complicated than to what I am used to when dealing with threads.

PS all those threads are QThreads from PyQt if that is of any importance but I doubt it.

thanks

kosta5
  • 1,129
  • 3
  • 14
  • 36
  • Make sure all those other threads have started, then do `for t in other_threads: t.wait()` will block until other_threads have completed (http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#wait). Use `t.join()` if using the `threading` module. – Steven Rumbalski Apr 19 '12 at 22:03

1 Answers1

1

The Queue module is great for communicating between threads without worrying about locks or other mutexes. It features a pair of methods, task_done() and join() that are used to signal the completion of tasks and to wait for all tasks to be completed. Here's an example from the docs:

def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

q = Queue()
for i in range(num_worker_threads):
     t = Thread(target=worker)
     t.daemon = True
     t.start()

for item in source():
    q.put(item)

q.join()       # block until all tasks are done
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • I think he just wants to know when these threads are done. `for t in threads: t.join()` (Or in the case of `QThreads`, `.wait()`) would work fine for that. – Steven Rumbalski Apr 19 '12 at 22:10
  • I ended up using a simple counter that I access in mutex. Once that counter is equal to number of threads I call my function. So the workflow goes like this: 1. start all threads 2. each of the little threads calls my method once nearning finish 3. it adds 1 to global counter in mutex 4. once the global variable is equal to number of threads the code is executed – kosta5 Apr 21 '12 at 08:41