This article introduces some of the main methods of the main class Thread in the threading module, the example code is as follows:
'''
Created on 2012-9-7
@author: walfred
@module: thread.ThreadTest3
@description:
'''
import threading
class MyThread():
def __init__(self):
.__init__(self)
def run(self):
print "I am %s" % ()
if __name__ == "__main__":
for i in range(0, 5):
my_thread = MyThread()
my_thread.start()
name-related
You can specify the name for each thread, and the default is of the form Thread-No, as printed in the example code above:
I am Thread-1
I am Thread-2
I am Thread-3
I am Thread-4
I am Thread-5
Of course you can specify the name of each thread, this is done through the setName method, code:
def __init__(self):
.__init__(self)
("new" + )
join method
The join method prototype is as follows. This method is used to block the current context until the thread has finished running:
def join(self, timeout=None):
timeout allows you to set a timeout
timeout allows you to set a timeout for nibbling.
setDaemon method
When we are running a program and execute a main thread, if the main thread creates another sub-thread, the main thread and the sub-thread are split into two, and when the main thread finishes and wants to exit, it will check if the sub-thread is finished. If the sub-thread is not finished, the main thread will wait for the sub-thread to finish before exiting. But sometimes we need is, as long as the main thread is completed, regardless of whether the sub-thread is completed, with the main thread to exit, then you can use the setDaemon method, and set its parameters to True.
Of course, the above list is only the methods we often use in programming, more methods, you can see:Higher-level threading interface