What is a Python threading module? 3 min To learn what a threading module is

Source: Internet
Author: User
This article mainly describes What is a threadThreadsis the smallest unit in which the operating system is capable of operation scheduling. Processes are included in the process and are the actual processing units in the process. A thread is a collection of instructions.

A thread is a single sequential control flow in a process that can have multiple threads in one process, and each thread performs different tasks in parallel.

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.

Creating a thread using the threading module

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): #继承父类threading. Threaddef __init__ (self, ThreadID, name, counter): Threading. Thread.__init__ (self) self.threadid = Threadidself.name = Nameself.counter = Counterdef Run (self): #把要执行的代码写到run函数里面 The thread runs the run function directly after it is created print "starting" + self.nameprint_time (Self.name, Self.counter, 5) 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) # Open thread Thread1.start () Thread2.start () print "Exiting Main Thread "

The results of the above procedure 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 Ma  R 09:10:06 2013thread-2: Thu Mar 09:10:06 2013thread-1: Thu Mar 09:10:07 2013Exiting thread-1thread-2: Thu Mar 21 09:10:08 2013thread-2: Thu Mar 09:10:10 2013thread-2: Thu Mar 09:10:12 2013Exiting Thread-2 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.