Created
July 14, 2019 14:01
-
-
Save eLtronicsVilla/dc7bea083080c67545eaf7a76ce4f905 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This python code is simple program for multi-threading | |
import threading | |
import os | |
def add(num1,num2): | |
# find the thread ID | |
print("Addition is assigned to thread: {}".format(threading.current_thread().name)) | |
print("ID of process running addition: {}".format(os.getpid())) | |
# This function used for addition of two number | |
print("addition result = ",num1+num2) | |
def mul(num1,num2): | |
print("Multiplication is assigned to thread: {}".format(threading.current_thread().name)) | |
print("ID of process running multiplication: {}".format(os.getpid())) | |
# This function used for multiplication of two number | |
print("Multiplication result = ",num1*num2) | |
def div(num1,num2): | |
print("Multiplication is assigned to thread: {}".format(threading.current_thread().name)) | |
print("ID of process running div: {}".format(os.getpid())) | |
# This function used for floor operation | |
print("Floor division result = " , num1//num2) | |
if __name__ == "__main__": | |
print("main() is assigned to thread: {}".format(threading.main_thread().name)) | |
print("ID of process running addition: {}".format(os.getpid())) | |
# Creating thread | |
th1 = threading.Thread(target=add, args=(10,20),name='th1') | |
th2 = threading.Thread(target=mul,args=(30,40),name='th2') | |
th3 = threading.Thread(target=div,args=(200,10),name='th3') | |
# Start thread1 | |
th1.start() | |
# Start thread2 | |
th2.start() | |
# Start thread3 | |
th3.start() | |
#Once the threads start, the current program (you can think of it like a main thread) also keeps on executing. | |
# first wait for the completion of t1 and then t2. | |
# wait untill thread 1 is completely executed | |
th1.join() | |
# wait untill thread 2 is completely executed | |
th2.join() | |
# wait untill thread 3 is completely executed | |
th3.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment