python线程安全队列讲解

本文介绍了Python中线程安全的Queue模块,包括FIFO和LIFO队列,以及队列的常用方法如qsize、empty、full、put和get。在多线程环境下,Queue无需额外加锁即可确保线程安全。进一步探讨了生产者与消费者模式,通过队列作为缓冲区实现两者之间的同步。案例中,生产者负责将URL放入队列,消费者则从队列中取出并处理链接,例如下载图片。

一、线程安全队列

python内置的线程安全队列模块叫queue

python的Queue模块中提供了同步的、线程安全的队列类

FIFO(先进先出)队列的Queue(常用)

LIFO(后进先出)lifoQueue

可以使用队列来实现线程间的同步。

二、队列的常用方法

函数描述
qsize()返回队列大小
empty()判断队列是否为空
full()判断队列是否满了
get()从队列中获取先插入的数据
put()将一个数据放到队列中
  • qsize()返回队列大小
 
from queue import Queue
# 指定队列当中最多可存放5个数据
queue = Queue(5)
for num in range(5):
# put()将一个数据放到队列中
queue.put(num) # 队列中获取先插入的数据
print("当前队列的大小为:", queue.qsize())
-------------------------
当前队列的大小为: 5
  • empty()判断队列是否为空
  • full() 判断队列是否为满
 
from queue import Queue
# 指定队列当中最多可存放5个数据
queue = Queue(5)
for num in range(5):
# put()将一个数据放到队列中
queue.put(num) # 队列中获取先插入的数据
# 判断队列是否为空
print(queue.empty())
print(queue.full())
----------------------
False
True
  • put() 将一个数据放到队列中
  • get() 从队列中获取先插入的数据 

 
from queue import Queue
# 指定队列当中最多可存放5个数据
queue = Queue(5)
for num in range(5):
# put()将一个数据放到队列中
queue.put(num) # 队列中获取先插入的数据
print("当前队列的大小为:", queue.qsize())
# get()从队列中获取先插入的数据
for i in range(1,6):
print(queue.get(i))
-------------------------------------
当前队列的大小为: 5
0
1
2
3
4

个人博客推荐:白煮蛋的博客 

三、在多线程当中使用

Queue是线程安全的队列,在使用时无须加锁,可以再多线程当中直接使用

队列也是实现线程间同步的方式

 
from queue import Queue
import threading
import random
import time
def put_data(queue):
while True:
queue.put(random.randint(10,100))
time.sleep(1)
print(queue.qsize())
def get_data(queue):
while True:
print(f"已经获取到队列当中的--{queue.get()}--元素")
def main():
queue = Queue(10)
t1 = threading.Thread(target=put_data,args=(queue,))
t2 = threading.Thread(target=get_data,args=(queue,))
t1.start()
t2.start()
if __name__ == '__main__':
main()

四、生产者与消费者模式

产生数据的模块,就形象地称为生产者;而处理数据的模块,就称为消费者。

单单抽象出生产者和消费者,还够不上是生产者/消费者模式。该模式还需要有一个缓冲区处于生产者和消费者之间,作为一个中介。生产者把数据放入缓冲区,而消费者从缓冲区取出数据。

  • 缓冲区

如果制造数据的速度时快时慢,缓冲区的好处就体现出来了。当数据制造快的时候,消费者来不及处理,未处理的数据可以暂时存在缓冲区中。等生产者的制造速度慢下来,消费者再慢慢处理掉。

  • 队列==缓冲区

(一)生产者与消费者模式案例

 
import requests
from bs4 import BeautifulSoup
import os
from threading import Thread
from queue import Queue
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26"}
# 生产者类
class Page_Url(Thread):
# page_queue 生产者队列
# img_queue 消费者队列
def __init__(self,page_queue,img_queue):
super().__init__()
self.page_queue = page_queue
self.img_queue = img_queue
def run(self):
while True:
if self.page_queue.empty():
break
url = self.page_queue.get()
self.download_image(url)
def download_image(self,url):
resp = requests.get(url, headers=headers).text
# print(resp)
soup = BeautifulSoup(resp, "lxml")
image_list = soup.find_all("img", class_="ui image lazy")
for img in image_list:
title = img.get("title")
href = img.get("data-original")
self.img_queue.put((title,href))
# 消费者类
class Img_Url(Thread):
# page_queue 生产者队列
# img_queue 消费者队列
def __init__(self, page_queue, img_queue):
super().__init__()
self.page_queue = page_queue
self.img_queue = img_queue
def run(self):
while True:
data = self.img_queue.get()
title,href = data
try:
with open("./image/"+title+os.path.splitext(href)[-1],"wb") as f:
resp =requests.get(href).content
f.write(resp)
print(title,"保存成功!")
except OSError:
pass
# 判断两个队列是否全部清空
if self.page_queue.empty() and self.img_queue.empty():
break
def main():
# 创建两个队列
page_queue = Queue()
img_queue = Queue()
for num in range(1,6):
url = "https://www.fabiaoqing.com/biaoqing/lists/page/{}.html".format(num)
# 将前五页的url添加到生产者队列
page_queue.put(url)
t1 = Page_Url(page_queue,img_queue)
t1.start()
t2 = Img_Url(page_queue,img_queue)
t2.start()
if __name__ == '__main__':
main()

(二)步骤解析

1、主类main

(1)创建生产者与消费者队列

(2)将前5页的url添加到生产者队列

 
def main():
# 创建两个队列
page_queue = Queue()
img_queue = Queue()
for num in range(1,6):
url = "https://www.fabiaoqing.com/biaoqing/lists/page/{}.html".format(num)
# 将前五页的url添加到生产者队列
page_queue.put(url)
t1 = Page_Url(page_queue,img_queue)
t1.start()
t2 = Img_Url(page_queue,img_queue)
t2.start()

2、生产者类

(1)生产者队列while循环当中获取的前5页的链接,当队列为空,停止获取。

 
def run(self):
while True:
if self.page_queue.empty():
break
url = self.page_queue.get()
self.download_image(url)

(2)将获取的链接给到dowl_img

 
def download_image(self,url):
resp = requests.get(url, headers=headers).text
# print(resp)
soup = BeautifulSoup(resp, "lxml")
image_list = soup.find_all("img", class_="ui image lazy")
for img in image_list:
title = img.get("title")
href = img.get("data-original")

(3)将获取到的名称和链接添加到消费者队列

 
self.img_queue.put((title,href))

3、消费者类

(1)从队列当中获取图片名称和链接

 
def run(self):
while True:
data = self.img_queue.get()
title,href = data

(2)保存图片

 
try:
with open("./image/"+title+os.path.splitext(href)[-1],"wb") as f:
resp =requests.get(href).content
f.write(resp)
print(title,"保存成功!")
except OSError:
pass
# 判断两个队列是否全部清空
if self.page_queue.empty() and self.img_queue.empty():
break

好了, 以上是本文所有内容,希望对大家有所帮助,也希望大家对码农之家多多支持,你们的支持是我创作的动力!祝大家生活愉快!    

Python 中,`threading` 模块提供了用于多线程编程的接口,允许程序同时运行多个线程,从而提高程序的并发性。尽管 Python 的全局解释器锁(GIL)限制了同一时间只有一个线程执行 Python 字节码,但多线程仍然适用于 I/O 密集型任务,例如网络请求、文件读写等,因为这些任务在等待时可以释放 GIL,让其他线程运行。 ### 线程的基本工作原理 线程是操作系统调度的最小执行单元,一个进程可以包含多个线程,它们共享进程的内存空间和资源。Python 的 `threading` 模块封装了底层操作系统的线程管理,提供了跨平台的线程创建、启动和同步机制。 线程的生命周期包括以下几个状态:创建、就绪、运行、阻塞和终止。通过 `Thread` 类创建线程后,调用 `start()` 方法使其进入就绪状态,等待调度器分配 CPU 时间片执行。 ### 线程的创建与启动 通过 `threading.Thread` 类可以创建一个新的线程对象,指定目标函数和参数。以下是一个简单的线程创建和启动示例: ```python import threading def thread_job(): print(f'This is an added Thread, number is {threading.current_thread()}') def main(): added_thread = threading.Thread(target=thread_job) added_thread.start() print(f'Active thread count: {threading.active_count()}') print(f'Enumerate threads: {threading.enumerate()}') print(f'Current thread: {threading.current_thread()}') if __name__ == "__main__": main() ``` ### 线程的同步与 `join()` 方法 当主线程启动子线程后,默认情况下主线程不会等待子线程完成。为了确保主线程等待所有子线程执行完毕再继续执行,可以使用 `join()` 方法。例如: ```python import threading import time def thread_job(): print("T1 start") for i in range(10): time.sleep(0.1) print("T1 finish") def main(): added_thread = threading.Thread(target=thread_job, name="T1") added_thread.start() added_thread.join() print("all done") if __name__ == "__main__": main() ``` ### 多线程与性能对比 在 I/O 密集型任务中,多线程可以显著提高程序的执行效率。例如,在爬虫应用中,多个线程可以同时发起 HTTP 请求,而不是串行等待每个请求完成。以下是一个单线程与多线程爬虫的性能对比示例: ```python import blog_spider import threading import time def single_thread(): print("single_thread begin") for url in blog_spider.urls: blog_spider.craw(url) print("single_thread end") def multi_thread(): print("multi_thread begin") threads = [] for url in blog_spider.urls: threads.append(threading.Thread(target=blog_spider.craw, args=(url,))) for thread in threads: thread.start() for thread in threads: thread.join() print("multi_thread end") if __name__ == '__main__': start = time.time() single_thread() end = time.time() print(f"single thread cost: {end - start}") start = time.time() multi_thread() end = time.time() print(f"multi thread cost: {end - start}") ``` ### 多线程与队列结合实现任务调度 在实际应用中,多线程常与 `Queue` 模块结合使用,实现线程安全的任务队列。以下是一个使用 `Queue` 实现多线程求和的示例: ```python import threading from queue import Queue import copy import time def job(l, q): res = sum(l) q.put(res) def multithreading(l): q = Queue() threads = [] for i in range(4): t = threading.Thread(target=job, args=(copy.copy(l), q), name=f'T{i}') t.start() threads.append(t) [t.join() for t in threads] total = 0 for _ in range(4): total += q.get() print(total) if __name__ == '__main__': l = list(range(1000000)) s_t = time.time() multithreading(l) print(f'multithreading: {time.time() - s_t}') ``` ### 相关问题 1. Python 中的全局解释器锁(GIL)如何影响多线程程序的性能? 2. 如何在 Python 中实现线程间的通信与数据共享? 3. 多线程与多进程在 Python 中的应用场景有何不同? 4. 如何在 Python 中避免线程安全问题? 5. Python 中 `daemon` 线程的作用是什么?如何设置? [^1] [^2] [^3] [^4]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值