#Multithreaded program in Python
#import multiprocessing
import threading
passing series of numbers as array in code
nums =[12, 75, 30, 91, 49, 52, 76]
This thread will determine the average of the numbers
Python program to get average of a list
def Average():
return round((sum(nums) / len(nums)) , 2)
Driver Code
average = Average()
Printing average of the list
print("Average of the list = " +str(average))
This thread will determine the maximum value
def maximum():
return max(nums)
Driven code
print("Maximum number in the given list is = "+ str(maximum()))
This thread will determine the minimum value
def minimum():
return min(nums)
Driven code
print("Minimum number in the given list is = "+ str(minimum()))
Creating objects for executing the threads
t1 = threading.Thread(target=Average, name=‘t1’)
t2 = threading.Thread(target=maximum, name=‘t2’)
t3 = threading.Thread(target=minimum, name=‘t3’)
t1 = Average()
t2 = maximum()
t3 = minimum()
Executing the threads, The 3 threads will store the values globally for
#the parent thread to output.
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
The problem is in these lines:
t1 = threading.Thread(target=Average, name=‘t1’)
t2 = threading.Thread(target=maximum, name=‘t2’)
t3 = threading.Thread(target=minimum, name=‘t3’)
t1 = Average()
t2 = maximum()
t3 = minimum()
In the first 3 lines you create the threads and assign the variables t1, t2 and t3 to them. However in the last 3 lines you reassign these variables to be the return value of the functions Average() etc.
Looking at the Average() function:
def Average():
return round((sum(nums) / len(nums)) , 2)
The round function in Python returns a floating point number which gets assigned to t1 (for instance: 10.0). Then you proceed to call the start function on the variable t1, which represents a float at this moment:
t1.start()
As a floating point does not have a start() function, it throws an AttributeError!
Hope this helps!
mkanda
June 4, 2022, 9:09am
#3
Yes tried what you suggested and removed the lines reassigning the variables, the error is gone
import threading
nums =[12, 75, 30, 91, 49, 52, 76]
def Average(nm):
return sum(nm) / len(nm)
average = Average(nums)
print("Average of the list = " +str(average))
def maximum(mx):
return max(mx)
print("Maximum number in the given list is = "+ str(maximum(nums)))
def minimum(mn):
return min(mn)
print("Minimum number in the given list is = "+ str(minimum(nums)))
t1 = threading.Thread(target=Average, args=(nums,))
t2 = threading.Thread(target=maximum, args=(nums,))
t3 = threading.Thread(target=minimum, args=(nums,))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
1 Like