Question: SOB 83: Understanding the Threading module in python 0 Simple Example of the Threading module We shall look into a simple example to threading module,






SOB 83: Understanding the Threading module in python 0 Simple Example of the Threading module We shall look into a simple example to threading module, and then go in detail of working with threads. Note : The following examples are worked on environment with Python3 installed. Following is a simple example to create multiple threads using threading module. import threading def f(): print('Thread function\ ') return for i in ranget3): t = threading.Thread{target=f) t.start() Expected Output Thread function Thread function Thread function - Create a thread using the Threading module You can create a thread in one of the two following ways. 2. Passing a method to Thread constructor. def f(): print('Thread function\ ') return t = threading.Thread(target=f) 3. Overriding run() method in a subclass of threading. Thread. import threading Class CustomThread(threading.Thread): def run(self): print('Custom thread function.\ ') for i in range(3): t = CustomThread{) . Start a Thread using the Threading module A thread is started by applying start() method on the thread object. import threading import time def f(): printt'Thread running.\ ') return # start threads by passing function to Thread constructor for i in range(3): t = threading.Thread(target=f) t.start() Expected Output Thread running Thread running Thread running . Passing arguments to the function supplied to Thread To pass arguments to the function supplied to Thread constructor, pass args in the Thread constructor as shown below : import threading import time def f(i) : for p in range (3) : time . sleep (i+1) 100 print ( 'Thread #', i, "\ ") time . sleep (i) return # start threads by passing function to Thread constructor for i in range (3) : t = threading . Thread (target=f, args=(i, ) ) t. start () Expected Output Thread # 0 Thread # 1 Thread # 0 Thread # 2 Thread # 0 Thread # 1 Thread # 1 Thread # 2 Thread # 2SOB 84: More examples of understanding the Threading module in python . Checking if the is Thread Alive! threading. Thread.is_alive() could be used to check if the thread is alive or not. Thread.is_alive() returns True if the thread is alive, False if not alive. import threading import time def f(i) : time . sleep (i) return # threads t1 = threading . Thread (target=f, args=(1.2, ) , name="Thread#1") t1 . start () t2 = threading. Thread (target=f, args= (2.2, ) , name="Thread#2") t2 . start () for p in range (5) : time . sleep (p*0 . 5) print ( ' [ ' , time. ctime () , '] ', t1. getName (), t1. is_alive() ) print ( ' [ ' , time . ctime () , '] ', t2. getName () , t2. is_alive () ) 101 Expected output [ Wed Dec 11 17:58:54 2019 ] Thread#1 True [ Wed Dec 11 17:58:54 2019 Thread#2 True [ Wed Dec 11 17:58:55 2019 Thread#1 True [ Wed Dec 11 17:58:55 2019 Thread#2 True [ Wed Dec 11 17:58:56 2019 Thread#1 False [ Wed Dec 11 17:58:56 2019 Thread#2 True [ Wed Dec 11 17:58:57 2019 Thread#1 False [ Wed Dec 11 17:58:57 2019 Thread#2 False [ Wed Dec 11 17:58:59 2019 Thread#1 False [ Wed Dec 11 17:58:59 2019 ] Thread#2 False Show and discuss the above output with your lab tutor' Thread Name Thread name could be set or read using setNameO and getNameO methods. In a function being called inside a thread, to get the current thread, use threading.current_thread(). In the following example we shall use this method to get current thread obj ect. import threading import time def f(i): for p in range(3): time.sleep(i+1.5) print{threading.current_thread{J.getName{)) return # start threads by passing function to Thread constructor for i in range(3): t = threading.Thread(target=f, args=(i,)) t.setName( 'Thread#'+str(i) ) t.start() Expected Output Thread#0 Thread#1 Thread#0 Thread#2 Thread#0 Thread#1 Thread#2 Thread#1 Thread#2 Based on your gained knowledge 'om the lecture slides, explain to your lab tutor when does a Thread stop? Typical SOB 85: Advanced Multithreading programs in python Run and explain the obtained output from the following script import threading import time exitFlag = 0 class myThread (threading . Thread) : def init (self, threadID, name, counter) : threading . Thread._ init_(self) self . threadID = threadID self . name = name self . counter = counter def run (self) : print ("Starting " + self. name) print_time (self . name, self . counter, 5) print ("Exiting " + self. name) def print_time (threadName, delay, counter) : while counter: if exitFlag: threadName . exit () time . sleep (delay) print ("&s: $s" . (threadName, time . ctime (time . time () ) ) ) counter -= # Create new threads threadl = myThread (1, "Thread-1", 1) thread2 myThread (2, "Thread-2", 2 # Start new Threads threadl . start () thread2 . start () threadl . join () thread2 . join () print ("Exiting Main Thread") Typical
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
