一、概念
多线程(multithreading)是指在软件或者硬件上实现多个线程并发执行的技术,优点如下:
1、多线程可以把占据较长时间的任务放到后台处理
2、多线程可以使程序的运行速度加快
3、某些等待的任务实现(如用户输入、文件读写、网络收发数据等),多线程的使用可以释放一些珍贵的如内存占用资源等
每个独立的线程均有一个程序运行的入口、顺序执行序列和程序的出口。但线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
每个线程都有其本身的一组CPU寄存器,称为线程的上下文,该上下文反映了线程上次运行该线程的CPU寄存器的状态。
指令指针和堆栈指针寄存器是线程上下文中两个最重要的寄存器,线程总是在进程得到上下文中运行的,这些地址都用于标志拥有线程的进程地址空间中的内存。
线程可以被抢占(即中断),也可以当在其他线程正在运行时,线程暂时搁置(也称为睡眠),这就是线程的退让。
线程分为内核线程和用户线程,内核线程指由操作系统内核创建和撤销的线程,用户线程指不需要内核支持而在用户程序中创建和撤销的线程
Python3 线程中常用的两个模块为“_thread”和“threading”,推荐使用threading。Python3已废弃“thread”模块,改为“_thread”替代
二、线程创建
Python使用线程通常有两种方式:函数或者用类来包装线程对象
2.1、_thread模块创建线程
函数式:
调用_thread模块的start_new_thread()创建线程,具体语法为:
_thread.start_new_thread(function , args , kwargs)
参数说明:
function:线程函数
args:传递给线程函数的参数,必须是tuple(元组)类型
kwargs:可选参数
"""
使用_thread创建线程
"""
import _thread
import time
# 定义线程函数
def print_time(ThreadName , delay):
# 计数器初始值设置为0
count = 0
# 打印3次
while count < 3:
# 延时delay秒
time.sleep(delay)
# time.time()返回当前时间的时间戳;time.ctime()把时间戳(按秒计算的浮点数)转化为time.asctime()形式
print("%s:%s" % (ThreadName , time.ctime(time.time())))
count += 1
# 创建线程
try:
_thread.start_new_thread(print_time , ("Thread_1" , 3 ,))
_thread.start_new_thread(print_time , ("Thread_2" , 5 ,))
except:
print("线程创建失败~~~")
# 无限循环
while 1:
pass
运行结果:
Thread_1:Sun Aug 9 21:47:52 2020
Thread_2:Sun Aug 9 21:47:54 2020
Thread_1:Sun Aug 9 21:47:55 2020
Thread_1:Sun Aug 9 21:47:58 2020
Thread_2:Sun Aug 9 21:47:59 2020
Thread_2:Sun Aug 9 21:48:04 2020
三、线程模块
Python3提供了“_thread”和“threading”两个标准库实现对线程的支持,但“_thread”提供了低级别的、原始的线程以及一个简单的锁,“threading”相比“_thread”功能丰富得多,其提供如下方法:
1、threading.currentThread():返回当前的线程变量
2、threading.enumerate():返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程
3、threading.activeCount():返回正在运行的线程数量,与len(threading.enumerate())有相同的结果
除以上提供的方法外,线程模块也提供了Thread类来处理线程,Thread类提供了以下方法:
1、run():用以表示线程活动的方法
2、start():启动线程活动
3、join(time):等待至线程中止。其阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生
4、isAlive():返回线程是否活动的
5、getName():返回线程名
6、setName():设置线程名
四、threading 模块创建线程
"""
使用 threading 创建线程
从 threading.Thread 继承创建一个新的子类,实例化后调用 start() 方法启动新线程
"""
import threading , time
# 退出标识,默认退出
exitFalg = 0
# 创建定义子类,继承自 threading.Thread 类
class myThreadClass(threading.Thread):
# 定义一个构造方法,类实例化时被调用
def __init__(self , name , counter , delay):
# 调用 threading.Thread 类构造方法
threading.Thread.__init__(self)
self.name = name
self.counter = counter
self.delay = delay
# 定义run()方法
def run(self):
print("开始线程:" , self.name)
# 调用print_time()方法
print_time(self.name , self.counter , self.delay)
print("停止线程:", self.name)
# 定义print_time()方法,输出时间
def print_time(ThreadName , counter , delay):
while counter:
if exitFalg:
# 退出线程
ThreadName.exit()
# 延时delay秒
time.sleep(delay)
print("%s :%s" % (ThreadName , time.ctime(time.time())))
counter = counter - 1
# myThreadClass实例化创建线程
myThread_1 = myThreadClass("Thread_1" , 2 , 3) # 循环2次,间隔3秒
myThread_2 = myThreadClass("Thread_2" , 3 , 4) # 循环3次,间隔4秒
# 开启线程
myThread_1.start()
myThread_2.start()
myThread_1.join()
myThread_2.join()
print ("退出主线程")
执行结果:
开始线程: Thread_1
开始线程: Thread_2
Thread_1 : Sun Aug 9 23:17:48 2020
Thread_2 : Sun Aug 9 23:17:49 2020
Thread_1 : Sun Aug 9 23:17:51 2020
停止线程: Thread_1
Thread_2 : Sun Aug 9 23:17:53 2020
Thread_2 : Sun Aug 9 23:17:57 2020
停止线程: Thread_2
退出主线程
五、线程同步
实际工作中经常遇到多个线程同时需要对某个数据进行修改的情况(如:列表里所有元素都是0,线程"set"从后向前把所有元素改成1,线程"print"负责从前往后读取列表并打印),此时可能出现不可预料的结果,为了保证数据的正确性,引入锁的概念。
使用 Thread 对象的 Lock 和 Rlock 可以实现简单的线程同步,这两个对象都有 acquire 方法和 release 方法,对于那些需要每次只允许一个线程操作的数据,可以将其操作放到 acquire 和 release 方法之间
**锁有两种状态:锁定和未锁定。**当线程"set"要访问共享数据时,必须先获得锁定;如果已有别的线程如"print"获得锁定了,那么先让线程"set"暂停,即同步阻塞;等到线程"print"访问完毕,释放锁以后,再让线程"set"继续。
"""
使用 threading 创建线程
从 threading.Thread 继承创建一个新的子类,实例化后调用 start() 方法启动新线程
"""
import threading , time
# 退出标识,默认退出
exitFalg = 0
# 创建锁对象,用于锁的获取和释放
threadLock = threading.Lock()
# 创建定义子类,继承自 threading.Thread 类
class myThreadClass(threading.Thread):
# 定义一个构造方法,类实例化时被调用
def __init__(self , name , counter , delay):
# 调用 threading.Thread 类构造方法
threading.Thread.__init__(self)
self.name = name
self.counter = counter
self.delay = delay
# 定义run()方法
def run(self):
print("开始线程:" , self.name)
# 获取锁,用于线程同步
threadLock.acquire()
# 调用print_time()方法
print_time(self.name , self.counter , self.delay)
# 释放锁,开启下一个线程
threadLock.release()
print("停止线程:", self.name)
# 定义print_time()方法,输出时间
def print_time(ThreadName , counter , delay):
while counter:
if exitFalg:
# 退出线程
ThreadName.exit()
# 延时delay秒
time.sleep(delay)
print("%s : %s" % (ThreadName , time.ctime(time.time())))
counter = counter - 1
# myThreadClass实例化创建新线程
myThread_1 = myThreadClass("Thread_1" , 2 , 3) # 循环2次,间隔3秒
myThread_2 = myThreadClass("Thread_2" , 3 , 4) # 循环3次,间隔4秒
# 添加线程到线程列表
threads = []
threads.append(myThread_1)
threads.append(myThread_2)
# # 开启新线程,同时等待所有线程完成
for t in threads:
t.start()
t.join()
print ("退出主线程")
执行结果:可见执行结果是myThread_1执行完再执行myThread_2
开始线程: Thread_1
Thread_1 : Sun Aug 9 23:46:36 2020
Thread_1 : Sun Aug 9 23:46:39 2020
停止线程: Thread_1
开始线程: Thread_2
Thread_2 : Sun Aug 9 23:46:43 2020
Thread_2 : Sun Aug 9 23:46:47 2020
Thread_2 : Sun Aug 9 23:46:51 2020
停止线程: Thread_2
退出主线程
六、线程优先级队列(Queue)
Python 的 Queue 模块提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue、LIFO(后入先出)队列LifoQueue和优先级队列 PriorityQueue,这些队列都实现了锁原语,能够在多线程中直接使用,可以使用队列来实现线程间的同步。
Queue 模块中的常用方法:
1、Queue.qsize():返回队列的大小
2、Queue.empty():如果队列为空,返回True,反之False
3、Queue.full():如果队列满了,返回True,反之False
4、Queue.full 与 maxsize 大小对应
5、Queue.get(block , timeout):获取队列 timeout 等待时间
6、Queue.get_nowait() :相当Queue.get(False)
7、Queue.put(item):写入队列 timeout 等待时间
8、Queue.put_nowait(item):相当Queue.put(item , False)
9、Queue.task_done():在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
10、Queue.join():实际上意味着等到队列为空,再执行别的操作
import queue
import threading
import time
# 定义退出标识,默认退出
exitFlag = 0
# 创建定义子类,继承自 threading.Thread 类
class myThread (threading.Thread):
# 定义一个构造方法,类实例化时被调用
def __init__(self, threadID, name, q):
# 调用 threading.Thread 类构造方法
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
# 定义run()函数
def run(self):
print ("开启线程:" + self.name)
process_data(self.name, self.q)
print ("退出线程:" + self.name)
def process_data(threadName, q):
while not exitFlag:
# 获取锁,用于线程同步
queueLock.acquire()
# 队列不为空
if not workQueue.empty():
# 获取队列
data = q.get()
# 释放锁
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
# 释放锁
queueLock.release()
time.sleep(2)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
# 创建锁对象,用于锁的获取和释放
queueLock = threading.Lock()
#workQueue = queue.Queue(10) # 定义先入先出队列
#workQueue = queue.LifoQueue(10) # 定义后入先出队列
workQueue = queue.PriorityQueue(10) # 定义优先级队列
threads = []
threadID = 1
# 创建新线程
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
# 启动线程
thread.start()
threads.append(thread)
threadID += 1
# 填充队列
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# 等待队列清空
while not workQueue.empty():
pass
# 通知线程是时候退出
exitFlag = 1
# 等待所有线程完成
for t in threads:
t.join()
print ("退出主线程")
执行结果:
# 先入先出队列Queue
开启线程:Thread-1
开启线程:Thread-2
开启线程:Thread-3
Thread-3 processing Five
Thread-2 processing Four
Thread-1 processing Three
Thread-1 processing Two
Thread-3 processing One
退出线程:Thread-1
退出线程:Thread-2
退出线程:Thread-3
退出主线程
# 先入先出队列LifoQueue
开启线程:Thread-1
开启线程:Thread-2
开启线程:Thread-3
Thread-2 processing One
Thread-1 processing Two
Thread-3 processing Three
Thread-2 processing Four
Thread-3 processing Five
退出线程:Thread-1
退出线程:Thread-2
退出线程:Thread-3
退出主线程
# 优先级队列 PriorityQueue
开启线程:Thread-1
开启线程:Thread-2
开启线程:Thread-3
Thread-2 processing Five
Thread-3 processing Four
Thread-1 processing One
Thread-2 processing Three
Thread-3 processing Two
退出线程:Thread-2
退出线程:Thread-3
退出线程:Thread-1
退出主线程
七、线程传递参数方式
7.1、元组传递 threading.Thread(target=方法名,args=(参数1,参数2, …))
注意:当args仅有一个参数时,括号内必须加逗号,即:args = (参数1 , ),元组内参数顺序必须与线程函数内参数顺序一致