Multi-threading program in python

0 votes

Could you share an example of a multithreading program?

Feb 7, 2019 in Python by Gani
688 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 7, 2019 by Omkar
• 69,220 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,560 points
1,803 views
0 votes
2 answers

How to use threading in Python?

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

answered Apr 6, 2019 in Python by anonymous
1,375 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, 2019 in Python by Mohammad
• 3,230 points
1,231 views
0 votes
2 answers

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

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,883 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,466 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,127 views
0 votes
3 answers

Python program to print occurrence of character in string

Try below code string = input("Enter a string: ...READ MORE

answered May 28, 2020 in Python by Anubhav
• 300 points
30,822 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP