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 »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.