0

I am very new to the concept of threading and the concepts are still somewhat fuzzy.

But as of now i have a requirement in which i spin up an arbitrary number of threads from my Python program and then my Python program should indicate to the user running the process which threads have finished executing. Below is my first try:

import threading
from threading import Thread
from time import sleep

def exec_thread(n):
    name = threading.current_thread().getName()
    filename = name + ".txt"
    with open(filename, "w+") as file:
        file.write(f"My name is {name} and my main thread is {threading.main_thread()}\n")
        sleep(n)
        file.write(f"{name} exiting\n")


t1 = Thread(name="First", target=exec_thread, args=(10,))
t2 = Thread(name="Second", target=exec_thread, args=(2,))

t1.start()
t2.start()

while len(threading.enumerate()) > 1:
    print(f"Waiting ... !")
    sleep(5)

print(f"The threads are done"

So this basically tells me when all the threads are done executing.

But i want to know as soon as any one of my threads have completed execution so that i can tell the user that please check the output file for the thread.

I cannot use thread.join() since that would block my main program and the user would not know anything unless everything is complete which might take hours. The user wants to know as soon as some results are available.

Now i know that we can check individual threads whether they are active or not by doing : thread.isAlive() but i was hoping for a more elegant solution in which if the child threads can somehow communicate with the main thread and say I am done !

Many thanks for any answers in advance.

Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60

1 Answers1

0

The simplest and most straightforward way to indicate a single thread is "done" is to put the required notification in the thread's implementation method, as the very last step. For example, you could print out a notification to the user.

Or, you could use events, see: https://docs.python.org/3/library/threading.html#event-objects

This is one of the simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it.

An event object manages an internal flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true.

So, the "final act" in your thread implementation would be to set an event object, and your main thread can wait until it's set.

Or, for an even fancier and more mechanism, use queues: https://docs.python.org/3/library/queue.html

Each thread writes an "I'm done" object to the queue when done, and the main thread can read those notifications from the queue in sequence as each thread completes.

payne
  • 13,833
  • 5
  • 42
  • 49
  • Can i create an event for each thread in which only the main thread waits for it ? Or would i have to make all the threads wait for the event to happen ? Sorry for the naive question but i am very new to this. – Subhayan Bhattacharya Aug 26 '18 at 16:47