1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 import thread 5 import time 6 7 8 # 为线程定义一个函数 9 def print_time(threadName, delay): 10 count = 0 11 while count < 5: 12 time.sleep(delay) 13 count += 1 14 print "%s: %s" % (threadName, time.ctime(time.time())) 15 16 17 # 创建两个线程 18 try: 19 thread.start_new_thread(print_time, ("Thread-1", 2,)) 20 thread.start_new_thread(print_time, ("Thread-2", 4,)) 21 except: 22 print "Error: unable to start thread" 23 24 while 1: 25 pass