kosta5
kosta5

Reputation: 1138

python threads synchronization

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

Upvotes: 0

Views: 679

Answers (1)

Raymond Hettinger
Raymond Hettinger

Reputation: 226654

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

Upvotes: 1

Related Questions