python只多线程二

本文介绍了Python多线程中的信号量,它限制了并发线程的数量,类似停车场车位显示。接着讨论了条件变量,用于控制线程的执行顺序,例如在有依赖关系的任务中。还提到了事件,通过标志位实现线程间的通信。最后,讲解了线程安全的队列,提供便捷的数据入出顺序控制,并在内部实现了锁的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

信号量也是锁的一种,这种锁会限制线程并发的个数。内有存在一个计数器。类似于停车场上面的车位显示屏,如果显示屏显示为4,意味同时只能进入4辆车进行停车。这种信号量也是一样的,比如count计数器为4,进来一个线程,count减1,直到count减到0,阻止线程进入。举一个代码实例:

import threading,time

class myThread(threading.Thread):

    def run(self):
        if semaphore.acquire():
            print(self.name)
            time.sleep(5)
            semaphore.release()

if __name__ == '__main__':
    semaphore = threading.BoundedSemaphore(5)
    thrs=[]
    for i in range(23):
        thrs.append(myThread())
    for t in thrs:
        t.start()

当运行这段代码程序时,每次都是启动5个线程并行运行,其他线程处于阻塞状态。当有其他线程释放的时候,处于阻塞状态的线程进入执行。

条件变量:在现实中,经常有这种场景出现,两个线程的执行是有先后顺序的,一个线程的执行要依赖于另一个线程执行完毕。这种场景我们就可以通过条件变量进行控制。

import threading,time
import random


class Producer(threading.Thread):
    def run(self):
        global  L
        while True:
            val = random.randint(0,100)
            print('生产者',self.name,":append"+str(val),L)
            if lock_con.acquire():
                #L.append(val)
                lock_con.notify()
                lock_con.release()
            time.sleep(3)


class Consumer(threading.Thread):
    def run(self):
        global L
        while True:
            lock_con.acquire()
            print('我拿到线程的执行权限了')
            if len(L) == 0:
                lock_con.wait()#这里等到信号后重新从acquire()开始执行
            print('消费者', self.name, ":Delete" + str(L[0]), L)
            del L[0]
            lock_con.release()
            time.sleep(1)


if __name__ == '__main__':
    L=[]
    lock_con = threading.Condition()
    threads=[]
    for i in range(5):
        threads.append(Producer())
    threads.append(Consumer())
    for t in threads:
        t.start()
    for t in threads:
        t.join()

    print('-------生产包子吃包子活动已经结束了----')

以上实例为做包子吃包子的动作,得先有包子,才能吃包子,这种条件变量主要运用进程间的通信。

事件:事件也是用于进程见的相互通信,只不过他是通过标志位进行线程间通信的,通过设置标志位,读取标志位来进行通信的,当标志位为false时,线程将被阻塞。为true时线程将会继续执行。

import threading,time
import random


class Boss(threading.Thread):
    def run(self):
        print('BOSS: 今晚大家都要加班到24:00.')
        event.is_set() or event.set()
        time.sleep(5)
        print('BOSS: <24:00>可以下班了。')
        event.is_set() or event.set()


class Worker(threading.Thread):
    def run(self):
        print('今天怎么个情况')
        event.wait()
        print('worker: 哎-----命苦啊')
        time.sleep(1)
        event.clear()
        event.wait()
        print("worker: OH YEAH 哈哈哈")



if __name__ == '__main__':
    event = threading.Event()
    threads=[]
    for i in range(2):
        threads.append(Worker())
    threads.append(Boss())
    for t in threads:
        t.start()
    for t in threads:
        t.join()

队列:队列是一个非常不错的数据结构,在队列内部可以定义数据的出入顺序。队列是线程安全的,且内部自动带一把锁。通过put 和get函数,参数设置0 为报错,为1时等待。

import threading,time
import queue
import random

class  Producer(threading.Thread):

    def run(self):
        while True:
            value = random.randint(0,100)
            d.put(value)
            print('producer 生产'+str(value)+'包子')
            time.sleep(5)

class Costumer(threading.Thread):

    def run(self):
        while True:
            d.get()
            print('costum 吃'+str(d.get())+'包子')

if __name__=='__main__':

    d = queue.Queue(10)
    threads=[]
    for i in range(5):
        t = Producer()
        t.start()
        threads.append(t)
    t1 = Costumer()
    t1.start()
    threads.append(t1)

    for t in threads:
        t.join()

通过队列我们就不用给他上锁,内部锁就帮我们解决了。使用起来更加的方便。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值