import thread
thread.start_new_thread ( function, args[, kwargs] )
# function: 线程函数。
# args: 传递给线程函数的参数,他必须是个tuple类型。
# kwargs: 可选参数。
##################################例子
import thread
import time
# 为线程定义一个函数
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s" % ( threadName, time.ctime(time.time()) )
thread.exit() #通过抛出异常SystemExit Exception来退出异常。
# 创建两个线程
try:
thread.start_new_thread( print_time, ("Thread-1", 2, ) )
thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
print "Error: unable to start thread"
while 1:
pass