Home >Backend Development >Python Tutorial >What is the python thread module? Nine ways to help you understand the thread module
For those who are exposed to the python programming language for the first time, when they just started to learn python thread module, they have an understanding of the thread module in python There are relatively few. In this article, let’s learn about the python thread module. Without further ado, let’s start learning the thread module.
What is the thread module
Python provides support for threads through two standard libraries, thread and threading. thread provides low-level, primitive threads 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 containing running threads. Running refers to after the thread starts and before it ends, excluding threads before starting and after termination.
3.threading.activeCount(): Returns the number of running threads, which has the same result as len(threading.enumerate()).
In addition to usage methods, the thread module also provides the Thread class to handle threads. The Thread class provides the following methods:
1.run(): Method used to represent thread activity.
2.start(): Start thread activity.
3.join([time]): Wait until the thread terminates. This blocks the calling thread until the thread's join() method is called abort - exit normally or throw an unhandled exception - or an optional timeout occurs.
4.isAlive(): Returns whether the thread is active.
5.getName(): Returns the thread name.
6.setName(): Set the thread name.
Use the Threading module to create threads
Use the Threading module to create threads, inherit directly from threading.Thread, and then override the __init__ method and run method:
# !/usr/bin/python # -*- coding: UTF-8 -*- import threading import time exitFlag = 0 class myThread(threading.Thread): # 继承父类threading.Thread def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 print "Starting " + self.name print_time(self.name, self.counter, 5) print "Exiting " + self.name def print_time(threadName, delay, counter): while counter: if exitFlag: (threading.Thread).exit() time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1 # 创建新线程 thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) # 开启线程 thread1.start() thread2.start() print "Exiting Main Thread"
The execution results of the above program are as follows:
Starting Thread-1 Starting Thread-2 Exiting Main Thread Thread-1: Thu Mar 21 09:10:03 2013 Thread-1: Thu Mar 21 09:10:04 2013 Thread-2: Thu Mar 21 09:10:04 2013 Thread-1: Thu Mar 21 09:10:05 2013 Thread-1: Thu Mar 21 09:10:06 2013 Thread-2: Thu Mar 21 09:10:06 2013 Thread-1: Thu Mar 21 09:10:07 2013 Exiting Thread-1 Thread-2: Thu Mar 21 09:10:08 2013 Thread-2: Thu Mar 21 09:10:10 2013 Thread-2: Thu Mar 21 09:10:12 2013 Exiting Thread-2
The above is all the content described in this article. This article mainly introduces the relevant knowledge of python thread module. I hope you can Ability to use information to understand what is said above. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
For more related knowledge, please visit the Python tutorial column on the php Chinese website.
The above is the detailed content of What is the python thread module? Nine ways to help you understand the thread module. For more information, please follow other related articles on the PHP Chinese website!