python 内置模块queue,队列

一、简介

多个线程之间的数据是共享的,多个线程进行数据交换的时候,不能够保证数据的安全性和一致性,所以当多个线程需要进行数据交换的时候,队列就出现了,队列可以完美解决线程间的数据交换,保证线程间数据的安全性和一致性。

二、队列类型
  1. queue.Queue(maxsize=0) 先进先出
    import random
    
    
    queue_data = [1,2,3,4,5,6]
    random.shuffle(queue_data)
    print('原数据:',queue_data)
    
    queue_test = queue.Queue()
    
    def queuePut(value):
        queue_test.put(value)
    
    for i in queue_data:
        queuePut(i)
    
    for i in range(len(queue_data)):
        print(queue_test.get())
    
  2. queue.LifoQueue(maxsize=0) 先进后出
    import queue
    import random
    
    
    queue_data = [1,2,3,4,5,6]
    random.shuffle(queue_data)
    print('原数据:',queue_data)
    
    queue_test = queue.LifoQueue()
    
    def queuePut(value):
        queue_test.put(value)
    
    for i in queue_data:
        queuePut(i)
    
    for i in range(len(queue_data)):
        print(queue_test.get())
    
    
  3. queue.PriorityQueue(maxsize=0) 优先级队列级别越低越先出来
    import queue
    import random
    
    
    queue_data = [1,2,3,4,5,6]
    random.shuffle(queue_data)
    print('原数据:',queue_data)
    
    queue_test = queue.PriorityQueue()
    
    def queuePut(value):
        queue_test.put(value)
    
    for i in queue_data:
        queuePut(i)
    
    for i in range(len(queue_data)):
        print(queue_test.get())
    
  4. queue.SimpleQueue(maxsize=0) 简化的队列,无跟踪任务的功能,只具有empty、get、put3个方法
三、队列方法
  1. queue.qsize() 返回队列的大小
  2. queue.empty() 如果队列为空,返回True,反之False
  3. queue.full() 如果队列满了,返回True,反之False
  4. queue.get(block,timeout) 获取队列,block:从队列里面取值,如果取不到值的话,程序不会结束, timeout:当block的值为真的时候,timeout是用来等待多少秒
  5. queue.put(value,block,timeout) 写入队列,value:写入队列的值,,block:队列如果满了的话,再往队列里放值的话会等待, timeout:当block的值为真的时候,timeout是用来等待多少秒
  6. queue.get_nowait() 相当于queue.get(False)
  7. queue.put_nowait(value) 相当于queue.put(value, False)
  8. queue.join() 阻塞调用线程,直到队列中的所有任务被处理掉, 实际上意味着等到队列为空,再执行别的操作
  9. queue.task_done() 在完成一项工作之后,queue.task_done()函数向任务已经完成的队列发送一个信号
  10. queue.full 与 maxsize 大小对应
四、队列+线程、队列+线程池

案例:简单的两人对话

  1. 队列+线程,会导致线程频繁的创建,且主线程不关闭,其他线程就会一直开启
    import queue
    import threading
    import time
    
    class ChatSend(threading.Thread):
        def __init__(self, name, message_queue):
            super().__init__()
            self.name = name
            self.message_queue = message_queue
    
        def run(self):
            '''使用队列中的任务跟踪'''
            message = input(f'{self.name}:')
            self.message_queue.put({
                'name': self.name,
                'message': message,
                'time': time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
            })
            self.message_queue.join()
    
    class ChatReceive(threading.Thread):
        def __init__(self, name, message_queue):
            super().__init__()
            self.name = name
            self.message_queue = message_queue
    
        def run(self):
            message_info = self.message_queue.get()
            print(f'\n{message_info["time"]}')
            print(f'{self.name}接收到{message_info["name"]}消息:{message_info["message"]}\n')
            self.message_queue.task_done()
    
    def chat(chat_person1, chat_person2):
        message_queue = queue.Queue()
    
        print(f'----------{chat_person1}和{chat_person2}正在对话----------')
    
        while True:
            send1 = ChatSend(chat_person1, message_queue)
            receive1 = ChatReceive(chat_person2, message_queue)
    
            send1.start()
            receive1.start()
            receive1.join()
    
            send2 = ChatSend(chat_person2,message_queue)
            receive2 = ChatReceive(chat_person1,message_queue)
    
            send2.start()
            receive2.start()
            receive2.join()
    
    
    chat('张三', '李四')
    
  2. 队列+线程+锁
    import queue
    import threading
    import time
    
    class ChatSend(threading.Thread):
        def __init__(self, name, message_queue, lock=None):
            super().__init__()
            self.name = name
            self.message_queue = message_queue
            self.lock = lock
    
        def run(self):
            self.lock.acquire()
            message = input(f'{self.name}:')
            self.message_queue.put({
                'name': self.name,
                'message': message,
                'time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            })
            self.lock.notify()
            self.lock.release()
            
    class ChatReceive(threading.Thread):
        def __init__(self, name, message_queue, lock=None):
            super().__init__()
            self.name = name
            self.message_queue = message_queue
            self.lock = lock
    
        def run(self):
            self.lock.acquire()
            self.lock.wait()
            message_info = self.message_queue.get()
            print(f'\n{message_info["time"]}')
            print(f'{self.name}接收到{message_info["name"]}消息:{message_info["message"]}\n')
            self.lock.release()
    
    
    def chat(chat_person1, chat_person2):
        message_queue = queue.Queue()
        lock = threading.Condition()
    
        print(f'----------{chat_person1}和{chat_person2}正在对话----------')
    
        while True:
            receive1 = ChatReceive(chat_person2, message_queue, lock)
            send1 = ChatSend(chat_person1, message_queue, lock)
    
            receive1.start()
            send1.start()
            receive1.join()
    
            receive2 = ChatReceive(chat_person1, message_queue, lock)
            send2 = ChatSend(chat_person2,message_queue, lock)
    
            receive2.start()
            send2.start()
            receive2.join()
    
    
    chat('张三', '李四')
    
  3. 队列+池,不会频繁创建线程
    import queue
    import threading
    import time
    from concurrent.futures import ThreadPoolExecutor,wait
    
    lock = threading.Condition()
    
    class ChatSend:
        def __init__(self, name, message_queue):
            self.send_person = name
            self.message_queue = message_queue
    
        def run(self):
            message = input(f'{self.send_person}:')
            self.message_queue.put({
                'name': self.send_person,
                'message': message,
                'time': time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
            })
            self.message_queue.join()
    
    class ChatReceive:
        def __init__(self, name, message_queue):
            self.receive_person = name
            self.message_queue = message_queue
    
        def run(self):
            message_info = self.message_queue.get()
            print(f'\n{message_info["time"]}')
            print(f'{message_info["name"]}对{self.receive_person}说:{message_info["message"]}\n')
            self.message_queue.task_done()
    
    
    class Chat(ThreadPoolExecutor):
        def __init__(self,person1, person2):
            ThreadPoolExecutor.__init__(self,max_workers=2)
    
            message_queue = queue.Queue()
    
            print(f'----------{person1}和{person2}正在对话----------')
    
            self.send1 = ChatSend(person1, message_queue)
            self.receive1 = ChatReceive(person2, message_queue)
    
            self.send2 = ChatSend(person2, message_queue)
            self.receive2 = ChatReceive(person1, message_queue)
    
        def start(self):
            while True:
                self.submit(self.send1.run)
                self.submit(self.receive1.run)
                self.submit(self.send2.run)
                self.submit(self.receive2.run)
    
    
    chat = Chat('张三', '李四')
    chat.start()
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

局外人LZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值