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?
- To fit the needs, first, you should know how to detect threads run completely in python3.
- In python code, we always create a thread and start it with
t1.start()
. - Then, we use
t1.join()
to wait for the thread to finish its execution. - 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")
- So we can see the function
join()
is just the function that will wait for the current thread to complete. - Now we will introduce the python thread join() function in more detail.
2. Python3 Thread join Method Introduction.
- The
join()
method in Python 3 is used to wait for a thread to finish its execution. - This method blocks the calling thread until the thread whose
join()
method is called finishes its execution. - This can be useful if you need to wait for a thread to finish before proceeding.
- The syntax for the
join()
method is as follows.thread.join([timeout])
- The
timeout
argument is optional and denotes the maximum number of seconds the calling thread should wait for the thread to finish its execution. - If the
timeout
argument is not given, the calling thread will wait indefinitely for the thread to finish. - In Python 3, the
join()
method returns theNone
value when it is called. - It is important to note that the
join()
method blocks the calling thread until the thread whosejoin()
method is called completes its execution. - 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?
- 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() ```