python_threading多线程、queue安全队列

简介: python_threading多线程、queue安全队列

多线程


image.png

image.png

threading库

常用方法

  • currentThread() 返回当前的线程变量
  • enumerate() 返回一个正在运行的线程list
  • activeCount() 返回正在运行的吸纳从数量(类似len(enumerate))

thread类

查询定义的thread类中start、run等方法

thread类

  • run() 线程活动的方法
  • start() 启动线程
  • join([time]) 阻塞调用线程直至线程的joiin()方法被调用终止
  • isActive 是否执行
  • getName() 但返回线程的名字
  • setName() 设置线程的名字

例:单线程、多线程分别执行两个函数的过程

import threading,time
def coding():
    for t in range(0,2):
        print('正在写python!')
        run_count()
        time.sleep(1)#延迟1s
def game():
    for t in range(0,2):
        print('游戏中!')
        run_count()
        time.sleep(1)#延迟1秒
def run_count():
    print('当前线程数量:',threading.active_count())
def single_thread():#单线程
    coding()
    game()
def muti_thread():#多线程
    fun1=threading.Thread(target=coding)
    fun2=threading.Thread(target=game)
    fun1.start()
    fun2.start()
if __name__=='__main__':
    print('单线程')
    single_thread()#
    print('多线程')
    muti_thread()

继承thread类

重构上面的代码

import threading,time
class codeWork(threading.Thread):#继承thread.Thread类
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name=name#名字自定义
    def run(self):
        the_thread=threading.current_thread()
        for i in range(2):
            print('正在写python!',the_thread.name)
            time.sleep(1)
class gameWork(threading.Thread):
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name=name#名字自定义
    def run(self):
        the_thread=threading.current_thread()
        for i in range(2):
            print('游戏中',the_thread.name)
            time.sleep(1)
def muti_thread():
    th1=codeWork('写代码的进程')
    th2=gameWork('游戏的进程')
    th1.start()
    th2.start()
if __name__=='__main__':
    muti_thread()

全局变量的问题

不加线程锁

因为多线程执行的不确定性,粗暴的更改全局变量可能会造成bug

例:多线程执行value++,查看全局变量value的值

import threading
value=0
class global_Work1(threading.Thread):#继承thread.Thread类
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name=name#名字自定义
    def run(self):
        the_thread=threading.current_thread()
        global value  # 定义全局变量
        for i in range(1,20):
            value+=1
            print('全局变量c:',value,'\t运行的进程:',threading.active_count(),the_thread.name)
class global_Work2(threading.Thread):#继承thread.Thread类
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name=name#名字自定义
    def run(self):
        the_thread=threading.current_thread()
        global value  # 定义全局变量
        for i in range(1,20):
            value+=1
            print('全局变量c:',value,'\t运行的进程:',threading.active_count(),the_thread.name)
def muti_thread():
    th1=global_Work1('全局变量测试1')
    th2 = global_Work1('全局变量测试2')
    th1.start()
    th2.start()
if __name__=='__main__':
    # global c  # 定义全局变量
    muti_thread()

添加线程锁Lock(线程同步)

threading.Lock()

  • acquire()
  • release()

queue线程安全队列

内置的线程安全模块queue(同步、安全,fifo)

  • qsize() 队列大小
  • empty() 队列是否为空
  • full() 队列是否满了
  • get()队列的最后一个数据
  • put() 数据加入队列

例:queue同步执行

import queue
import threading
import time
exitFlag = 0
class myThread(threading.Thread):
    def __init__(self, threadID, name, q):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.q = q
    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 运行 %s" % (threadName, data))
        else:
            queueLock.release()
        time.sleep(1)
if __name__=='__main__':
    threadList = ["Thread-1", "Thread-2", "Thread-3"]
    nameList = ["线程1", "线程2", "线程3", "线程4", "线程5"]  #
    queueLock = threading.Lock()  # 线程锁
    workQueue = queue.Queue(10)  # 大小为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("Exiting Main Thread")


目录
相关文章
|
7月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
698 0
|
9月前
|
数据采集 消息中间件 并行计算
Python多线程与多进程性能对比:从原理到实战的深度解析
在Python编程中,多线程与多进程是提升并发性能的关键手段。本文通过实验数据、代码示例和通俗比喻,深入解析两者在不同任务类型下的性能表现,帮助开发者科学选择并发策略,优化程序效率。
705 1
|
10月前
|
数据采集 监控 调度
干货分享“用 多线程 爬取数据”:单线程 + 协程的效率反超 3 倍,这才是 Python 异步的正确打开方式
在 Python 爬虫中,多线程因 GIL 和切换开销效率低下,而协程通过用户态调度实现高并发,大幅提升爬取效率。本文详解协程原理、实战对比多线程性能,并提供最佳实践,助你掌握异步爬虫核心技术。
|
安全 Python
告别低效编程!Python线程与进程并发技术详解,让你的代码飞起来!
【7月更文挑战第9天】Python并发编程提升效率:**理解并发与并行,线程借助`threading`模块处理IO密集型任务,受限于GIL;进程用`multiprocessing`实现并行,绕过GIL限制。示例展示线程和进程创建及同步。选择合适模型,注意线程安全,利用多核,优化性能,实现高效并发编程。
269 3
|
安全 数据安全/隐私保护 数据中心
Python并发编程大挑战:线程安全VS进程隔离,你的选择影响深远!
【7月更文挑战第9天】Python并发:线程共享内存,高效但需处理线程安全(GIL限制并发),适合IO密集型;进程独立内存,安全但通信复杂,适合CPU密集型。使用`threading.Lock`保证线程安全,`multiprocessing.Queue`实现进程间通信。选择取决于任务性质和性能需求。
425 1
|
数据采集 Java 数据处理
Python实用技巧:轻松驾驭多线程与多进程,加速任务执行
在Python编程中,多线程和多进程是提升程序效率的关键工具。多线程适用于I/O密集型任务,如文件读写、网络请求;多进程则适合CPU密集型任务,如科学计算、图像处理。本文详细介绍这两种并发编程方式的基本用法及应用场景,并通过实例代码展示如何使用threading、multiprocessing模块及线程池、进程池来优化程序性能。结合实际案例,帮助读者掌握并发编程技巧,提高程序执行速度和资源利用率。
737 0
|
并行计算 数据处理 调度
Python中的并发编程:探索多线程与多进程的奥秘####
本文深入探讨了Python中并发编程的两种主要方式——多线程与多进程,通过对比分析它们的工作原理、适用场景及性能差异,揭示了在不同应用需求下如何合理选择并发模型。文章首先简述了并发编程的基本概念,随后详细阐述了Python中多线程与多进程的实现机制,包括GIL(全局解释器锁)对多线程的影响以及多进程的独立内存空间特性。最后,通过实例演示了如何在Python项目中有效利用多线程和多进程提升程序性能。 ####
|
数据采集 存储 安全
如何确保Python Queue的线程和进程安全性:使用锁的技巧
本文探讨了在Python爬虫技术中使用锁来保障Queue(队列)的线程和进程安全性。通过分析`queue.Queue`及`multiprocessing.Queue`的基本线程与进程安全特性,文章指出在特定场景下使用锁的重要性。文中还提供了一个综合示例,该示例利用亿牛云爬虫代理服务、多线程技术和锁机制,实现了高效且安全的网页数据采集流程。示例涵盖了代理IP、User-Agent和Cookie的设置,以及如何使用BeautifulSoup解析HTML内容并将其保存为文档。通过这种方式,不仅提高了数据采集效率,还有效避免了并发环境下的数据竞争问题。
360 1
如何确保Python Queue的线程和进程安全性:使用锁的技巧
Python中的多线程与多进程
本文将探讨Python中多线程和多进程的基本概念、使用场景以及实现方式。通过对比分析,我们将了解何时使用多线程或多进程更为合适,并提供一些实用的代码示例来帮助读者更好地理解这两种并发编程技术。
|
数据挖掘 程序员 调度
探索Python的并发编程:线程与进程的实战应用
【10月更文挑战第4天】 本文深入探讨了Python中实现并发编程的两种主要方式——线程和进程,通过对比分析它们的特点、适用场景以及在实际编程中的应用,为读者提供清晰的指导。同时,文章还介绍了一些高级并发模型如协程,并给出了性能优化的建议。
212 3

推荐镜像

更多