The start_new_thread(function, args [, kwargs]) method in the Python thread module enables a fast and efficient way to create new threads in both Linux and Windows. It accepts a function name as the first parameter and a set of arguments as the second. The optional third parameter allows you to pass a dictionary containing keyword arguments. The start_new_thread method creates a new thread and then starts code execution of the function. Control is immediately returned to the calling thread, and the new thread executes the specified function and returns silently. Note If the code being executed by a new thread encounters an exception, a stack trace will be printed and the thread will exit. However, other threads will continue to run. Although it is very effective for low-level threading, the thread module is very limited compared to the newer threading module. import thread import time def print_time(threadName, delay): while 1: time.sleep(delay) print "%s: %s" % (threadName, \ time.ctime(time.time())) #Start threads to print time at different intervals thread.start_new_thread(print_time, ("Thread01", 2,)) thread.start_new_thread(print_time, ("Thread02", 4,)) while 1: pass create_thread.py Thread01: Wed Jun 14 12:46:21 2006 Thread01: Wed Jun 14 12:46:23 2006 Thread02: Wed Jun 14 12:46:23 2006 Thread01: Wed Jun 14 12:46:25 2006 Thread01: Wed Jun 14 12:46:27 2006 Thread02: Wed Jun 14 12:46:27 2006 Thread01: Wed Jun 14 12:46:29 2006 Thread01: Wed Jun 14 12:46:31 2006 . . . . . Output from create_thread.py code |