Multi-threading program in python

0 votes

Could you share an example of a multithreading program?

Feb 6 in Python by Gani
18 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 66,880 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
80 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
52 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,400 points
16 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,400 points
12 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
55 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
133 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
• 66,880 points
39 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.