How To Run Threads In A While True Loop And Only Restart Threads That Have Finished In Python3

I use python 3 to develop my project, and I run a function such as ‘test1‘ on three different threads. Each thread is locked until the function is completed, and a reset lock is created when the thread starts. I am aiming to have each thread start, loop through the code quickly, and restart threads that have finished while not restarting threads that have not finished until they do. To do this, I have attempted to create new dictionaries and update the thread states in the dictionary, but have not been successful. Can anyone tell me how to run threads in a while true loop and only restart threads that have finished in python3?

1. How To Detect Thread Run Completely In Python3?

  1. To fit the needs, first, you should know how to detect threads run completely in python3.
  2. In python code, we always create a thread and start it with t1.start().
  3. Then, we use t1.join() to wait for the thread to finish its execution.
  4. Below is the example source code, in the below code, once the thread has completed its execution, Thread 1 is complete will be printed out.
    # Create a thread
    import threading
    import time
    
    def func1():
        # Do some stuff
        print("Thread 1")
        time.sleep(5)
    
    # Create the thread
    t1 = threading.Thread(target=func1)
    
    # Start the thread
    t1.start()
    
    # Wait for the thread to finish
    t1.join()
    
    # Output
    print("Thread 1 is complete")
  5. So we can see the function join() is just the function that will wait for the current thread to complete.
  6. Now we will introduce the python thread join() function in more detail.

2. Python3 Thread join Method Introduction.

  1. The join() method in Python 3 is used to wait for a thread to finish its execution.
  2. This method blocks the calling thread until the thread whose join() method is called finishes its execution.
  3. This can be useful if you need to wait for a thread to finish before proceeding.
  4. The syntax for the join() method is as follows.
    thread.join([timeout])
  5. The timeout argument is optional and denotes the maximum number of seconds the calling thread should wait for the thread to finish its execution.
  6. If the timeout argument is not given, the calling thread will wait indefinitely for the thread to finish.
  7. In Python 3, the join() method returns the None value when it is called.
  8. It is important to note that the join() method blocks the calling thread until the thread whose join() method is called completes its execution.
  9. If multiple threads are waiting for the same thread to complete its execution, the program may enter a deadlock state. Therefore, it is important to use the join() method judiciously.

3. How To Run Threads In A While True Loop And Only Restart Threads That Have Finished In Python3?

  1. Below is the example source code that can fix this issue.
    ```Python
    while True:
        threads = []
        # Create threads
        for i in range(10):
            t = Thread(target=my_function,args=(i,))
            threads.append(t)
            t.start()
    
        # Wait for all threads to complete
        for t in threads:
            t.join()
    
        # Restart any threads that have finished
        for t in threads:
            if not t.is_alive():
                t = Thread(target=my_function,args=(i,))
                t.start()
    ```
    

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.