Answer to Question #222463 in Python for Dion Martins

Question #222463
1. Debug(find and correct errors) and run the Python script file Lab X,
2. Analyze the output and interpret what the script is about or does[reading comments may help]
import threading
import time
import random
def myThread(i):
print("Thread {}: started".format(i))
time.sleep(random.randint(1,5))
print("Thread {}: finished".format(i))
def main():
for i in range(4):
thread = threading.Thread(target=myThread, args=(i,))
thread.start()
print("Enumerating: {}".format(threading.enumerate()))
if __name__ == '__main__':
main()
"""In the preceding example, we start off by defining a function called myThread,
which will be the target of the threads that we are about to create.
Within this function, we simply print that the thread has started,
and then we wait for a random interval between 1 and 5 seconds
before printing that the thread is terminating.We have then defined a
main function which creates four distinct thread objects, and then
starts them off.
1
Expert's answer
2021-08-02T07:16:05-0400
#import threading module
import threading
#import time module
import time
#import random module
import random


#define myThread function taking i as an argument
def myThread(i):


    print("Thread {}: started".format(i))
    
    time.sleep(random.randint(1,5))
    
    print("Thread {}: finished".format(i))
#define the main function    
def main():


    for i in range(4):
        thread = threading.Thread(target=myThread, args=(i,))
        thread.start()
        print("Enumerating: {}".format(threading.enumerate()))


if __name__ == '__main__':
    #call main function
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS