For the first time in contact with the Python programming language of the friend, just began to learn
Python Threading ModuleWhen, for
Python Threads ModuleThere is less understanding of this aspect, and in this article we will learn about the Python threading module. Needless to say, let's start learning threading modules.
What is a threading module
Python provides support for threads through the two standard library thread and threading. The thread provides a low-level, primitive thread, and a simple lock.
Other methods provided by the threading module:
1.threading.currentthread (): Returns the current thread variable.
2.threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends.
3.threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).
In addition to using methods, the thread module also provides the thread class to handle threads, and the thread class provides the following methods:
1.run (): The method used to represent thread activity.
2.start (): Initiates thread activity.
3.join ([Time]): Waits until the thread aborts. This blocks the calling thread until the thread's join () method is called abort-gracefully exits or throws an unhandled exception-or an optional timeout occurs.
4.isAlive (): Returns whether the thread is active.
5.getName (): Returns the thread name.
6.setName (): Sets the thread name.
Using the threading module to create a thread
Use the threading module to create the thread directly from the threading. Thread inherits, and then overrides the __init__ method and the Run method:
#!/usr/bin/python#-*-coding:utf-8-*-import threadingimport timeexitflag = 0class myThread (threading. Thread): # inherits the parent class threading. Thread def __init__ (self, ThreadID, name, counter): Threading. Thread.__init__ (self) self.threadid = ThreadID self.name = name Self.counter = Counter def run (self): # Write the code you want to execute into the run function. The thread runs the run function directly after it is created print "starting" + self.name print_time (Self.name, Self.counter, 5) C10/>print "Exiting" + self.namedef print_time (threadname, Delay, counter): While counter: if Exitflag: (Threading. Thread). Exit () time.sleep (delay) print "%s:%s"% (ThreadName, Time.ctime (Time.time ())) Counter-= # Create a new thread Thread1 = MyThread (1, "Thread-1", 1) thread2 = MyThread (2, "Thread-2", 2) # Turn on Thread Thread1.start () Thread2.start () print " Exiting Main Thread "
The results of the above program execution are as follows:
starting thread-1starting thread-2exiting Main ThreadThread-1: Thu Mar 21 09:10:03 2013thread-1: Thu Mar 09:10:04 2013thread-2: Thu Mar 09:10:04 2013thread-1: Thu Mar 09:10:05 2013thread- 1:thu Mar 09:10:06 2013thread-2: Thu Mar 09:10:06 2013thread-1: Thu Mar 09:10:07 2013Exiting thread-1thread-2: T Hu Mar 09:10:08 2013thread-2: Thu Mar 09:10:10 2013thread-2: Thu Mar 09:10:12 2013Exiting Thread-2