python start new thread

Solutions on MaxInterview for python start new thread by the best coders in the world

showing results for - "python start new thread"
Andrés
16 Nov 2019
1import _thread
2import time
3
4# Define a function for the thread
5def print_time( threadName, delay):
6   count = 0
7   while count < 5:
8      time.sleep(delay)
9      count += 1
10      print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
11
12# Create two threads as follows
13try:
14   _thread.start_new_thread( print_time, ("Thread-1", 2, ) )
15   _thread.start_new_thread( print_time, ("Thread-2", 4, ) )
16except:
17   print ("Error: unable to start thread")
18
19while 1:
20   pass
21#The program will create two threads that will run the function print_time 
22#at the same time with the different values.