ADVERTISEMENT
ADVERTISEMENT

Python Program to illustrate parameterized threads

Here, we are going to see a Python program to pass arguments while invoking threads and then using it in threads.
Submitted by Shivang Yadav, on February 19, 2021

Python uses the threading library to support multithreading and some more functions. We can pass arguments while creating the thread which can then be used when the thread is running.

The passed parameters can be accessed using the array param[] that stores it.

Python program to illustrate parameterized threads

import threading
import time

def ProcessOne(*param):
    while(True):
        print(param[0],threading.current_thread().getName(),"is Running",param[1])
        time.sleep(param[2])

def ProcessTwo(*param):
    while(True):
        print(param[0],threading.current_thread().getName(),"is Running",param[1])
        time.sleep(param[2])

T1=threading.Thread(target=ProcessOne,name="Swift",args=('Maruti',200,1))
T2=threading.Thread(target=ProcessTwo,name='I20',args=('Hyundai',220,5))

T1.start()
T2.start()

Output:

Maruti Swift is Running 200
Hyundai I20 is Running 220
Maruti Swift is Running 200
Maruti Swift is Running 200
Maruti Swift is Running 200
Maruti Swift is Running 200
Hyundai I20 is Running 220
Maruti Swift is Running 200
Maruti Swift is Running 200
Maruti Swift is Running 200
Maruti Swift is Running 200
Maruti Swift is Running 200
Hyundai I20 is Running 220
...
...

Python Threading Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.