May-20-2020, 06:59 AM
We need to perform Hands-On - Multithreading in Python using global variable.
There is a global variable 'rv' which should have the entire message in the form of string. Concatenate to rv with + operator in the function each time it is supposed to give a message.
Don't forget the '\n' or newline at the end of each line of output given to the rv global variable.
The input will be two int each in a separate line. This will be send as argument to a wrapper function 'do_it()' where you should create the 2 threads with square_it as target. Each thread will be given one of the inputs as argument.
Inside square find the square of the argument convert it to string and concatenate it to the global variable.
Here we are expecting output as
4
9
We had written code as
There is a global variable 'rv' which should have the entire message in the form of string. Concatenate to rv with + operator in the function each time it is supposed to give a message.
Don't forget the '\n' or newline at the end of each line of output given to the rv global variable.
The input will be two int each in a separate line. This will be send as argument to a wrapper function 'do_it()' where you should create the 2 threads with square_it as target. Each thread will be given one of the inputs as argument.
Inside square find the square of the argument convert it to string and concatenate it to the global variable.
Here we are expecting output as
4
9
We had written code as
import os import sys import threading rv = "" def square_it(n): global rv rv += "\n" # Write your code here print(rv, n * n) def do_it(a, b): t1 = threading.Thread(target=square_it, args=(a,)) t2 = threading.Thread(target=square_it, args=(b,)) t1.start() t2.start() t1.join() t2.join() if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') a = int(input().strip()) b = int(input().strip()) do_it(a, b) fptr.write(rv + '\n') fptr.close()We are not getting required result. assuming there is a small mistake in code but really not understanding where is the mistake. Please help us on it.