Python Forum
Need help on Hands-On - Multithreading in Python using global variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help on Hands-On - Multithreading in Python using global variable
#1
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

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.
Reply
#2
Our Output coming as below, due to which our test failed

Run as Custom Input
2
3
Your Output (stdout)



Expected Output
4
9
Debug output
4


9
Reply
#3
What is rv? When you write to the file, what are you writing?
Reply
#4
rv is a global variable
Reply
#5
My question about rv is what do you expect it to hold. Right now there isn't anything but a '\n', Should this contain the squares?

What is os.environ['OUTPUT_PATH']? Is this "Your Output" or "Debug Output"? If this is "Your Output", you are not getting the expected output because you never add the squares to rv.
Reply
#6
You only ever put new line characters in rv (line 8). Why would you expect anything else? Line 10 of course doesn't modify the variable...

Also a few things:

1. Do you really need to use a global? Global mutable state leads to code that is hard to follow and maintain.

2. Race conditions can occur when you have mutable state shared across multiple threads. You'll want to be aware of those kinds of issues.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Money Global Variable saturnstars 8 4,199 Apr-12-2019, 10:48 AM
Last Post: ichabod801
  Global Variable issue AnOrdinaryFolk 1 2,256 Feb-06-2018, 07:26 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020