Multi-threading program in python

0 votes

Could you share an example of a multithreading program?

Feb 6 in Python by Gani
19 views

1 answer to this question.

0 votes
import threading

import os


def task1():

 print("Task 1 assigned to thread: {}".format(threading.current_thread().name))

 print("ID of process running task 1: {}".format(os.getpid()))


def task2():

 print("Task 2 assigned to thread: {}".format(threading.current_thread().name))

 print("ID of process running task 2: {}".format(os.getpid()))


if __name__ == "__main__":


 # print ID of current process

 print("ID of process running main program: {}".format(os.getpid()))


 # print name of main thread

 print("Main thread name: {}".format(threading.main_thread().name))


 # creating threads

 t1 = threading.Thread(target=task1, name='t1')

 t2 = threading.Thread(target=task2, name='t2')


 # starting threads

 t1.start()

 t2.start()


 # wait until all threads finish

 t1.join()

 t2.join()
answered Feb 6 by Omkar
• 67,120 points

Related Questions In Python

0 votes
1 answer

Is multi-threading supported in Python and can it speed up execution time as well?

The GIL does not prevent threading. All ...READ MORE

answered Nov 22, 2018 in Python by Nymeria
• 3,500 points
108 views
0 votes
2 answers

How to use threading in Python?

 Thread is the smallest unit of processing that ...READ MORE

answered Apr 6 in Python by anonymous
64 views
0 votes
1 answer

how do I check the length of an array in a python program?

lets say we have a list mylist = ...READ MORE

answered Mar 12 in Python by Mohammad
• 1,500 points
26 views
0 votes
1 answer

How can I write a program to add two numbers using functions in python?

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20 in Python by Mohammad
• 1,500 points
21 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 124 views
+4 votes
6 answers
+1 vote
2 answers

Python program to print occurrence of character in string

Try the below code inputString = input("Enter a ...READ MORE

answered Feb 26 in Python by Anubhav
• 160 points
204 views
+4 votes
6 answers

Use of "continue" in python

The break statement is used to "break" ...READ MORE

answered Jul 16, 2018 in Python by Omkar
• 67,120 points
112 views