//test.py
1 import thread
2 import time
3 def print_time(threadName, delay):
4 count = 0
5 while count<5:
6 time.sleep(delay)
7 count += 1
8 print '%s:%s' % (threadName, time.ctime(time.time()))
9 try:
10 thread.start_new_thread(print_time, ('Thread-1', 2))
11 thread.start_new_thread(print_time, ('Thread-2', 4))
12 except Exception, e:
13 print 'Error: unable to start thread:', e
14
15 while 1:
16 pass
//result
# python test.py
Thread-1:Wed Nov 15 01:13:48 2017
Thread-2:Wed Nov 15 01:13:50 2017
Thread-1:Wed Nov 15 01:13:50 2017
Thread-1:Wed Nov 15 01:13:52 2017
Thread-2:Wed Nov 15 01:13:54 2017
Thread-1:Wed Nov 15 01:13:54 2017
Thread-1:Wed Nov 15 01:13:56 2017
Thread-2:Wed Nov 15 01:13:58 2017
Thread-2:Wed Nov 15 01:14:02 2017
Thread-2:Wed Nov 15 01:14:06 2017