In this post, we will see how to join Threads and how to return a value from one of them.
We open Visual Studio Code and we create a file called main.py where, we will write this code:
import threading
import time
def Sum1(inputValue):
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum1 is: {count}")
time.sleep(1)
print(f"The final value in Sum1 is: {count}")
def Sum2(inputValue):
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum2 is: {count}")
time.sleep(1)
print(f"The final value in Sum2 is: {count}")
def Sum3(inputValue):
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum3 is: {count}")
time.sleep(1)
print(f"The final value in Sum3 is: {count}")
# Creating the threads
thread1 = threading.Thread(target=Sum1, args=(3,))
thread2 = threading.Thread(target=Sum2, args=(6,))
thread3 = threading.Thread(target=Sum3, args=(9,))
# Starting the thread
thread1.start()
thread2.start()
thread3.start()
If we run the application, the following will be the result:
Now, if we want to run the Thread thread3 only after thread1 and thread2 have been executed, we have to modify the code like this:
import threading
import time
def Sum1(inputValue):
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum1 is: {count}")
time.sleep(1)
print(f"The final value in Sum1 is: {count}")
def Sum2(inputValue):
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum2 is: {count}")
time.sleep(1)
print(f"The final value in Sum2 is: {count}")
def Sum3(inputValue):
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum3 is: {count}")
time.sleep(1)
print(f"The final value in Sum3 is: {count}")
# Create the threads
thread1 = threading.Thread(target=Sum1, args=(3,))
thread2 = threading.Thread(target=Sum2, args=(6,))
thread3 = threading.Thread(target=Sum3, args=(9,))
# Start the thread
thread1.start()
thread2.start()
thread1.join()
thread2.join()
thread3.start()
If we run the application, the following will be the result:
Finally, if we want to pass the result of the method Sum2 in the method sum3, we have to modify the code as follows:
import threading
import time
def Sum1(inputValue):
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum1 is: {count}")
time.sleep(1)
print(f"The final value in Sum1 is: {count}")
def Sum2(inputValue, result):
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum2 is: {count}")
time.sleep(1)
result[0] = count
print(f"The final value in Sum2 is: {count}")
def Sum3(inputValue):
print(f"The input value in Sum3 is: {inputValue}")
count = 0
for i in range(1, inputValue):
count += i
print(f"The current value in Sum3 is: {count}")
time.sleep(1)
print(f"The final value in Sum3 is: {count}")
# Create a shared variable to store the result...
# By using a shared variable, both the main thread and the Sum1 thread can access
# and update the value of result, allowing us to capture the return value from the Sum1
# function without using a class.
result = [None]
# Create the threads
thread1 = threading.Thread(target=Sum1, args=(3,))
thread2 = threading.Thread(target=Sum2, args=(6, result))
# Start the thread
thread1.start()
thread2.start()
thread1.join()
thread2.join()
thread3 = threading.Thread(target=Sum3, args=(result[0],))
thread3.start()
If we run the application, the following will be the result: