1. import thread
    2. thread.start_new_thread ( function, args[, kwargs] )
    3. # function: 线程函数。
    4. # args: 传递给线程函数的参数,他必须是个tuple类型。
    5. # kwargs: 可选参数。
    6. ##################################例子
    7. import thread
    8. import time
    9. # 为线程定义一个函数
    10. def print_time( threadName, delay):
    11. count = 0
    12. while count < 5:
    13. time.sleep(delay)
    14. count += 1
    15. print "%s: %s" % ( threadName, time.ctime(time.time()) )
    16. thread.exit() #通过抛出异常SystemExit Exception来退出异常。
    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. while 1:
    24. pass