Python Queue队列 优先级 本文共有5790个字,关键词: 实现了锁,并且自定义队列的大小,排列往队列中写入数据并且按先后顺序取出执行import datetime import threading import queue import time from time import sleep 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(f'{self.name}--start') process_data(self.name, self.q) print(f'{self.name}--exit') def process_data(threadName, q): while not exitFlag: #and not putFlag: queueLock.acquire() if not workQueue.empty(): data = q.get() print(f'{threadName} -- > get --> {data} ') queueLock.release() else: queueLock.release() sleep(0.01) def addDataToQueue(): nameList = ['name' + str(i) for i in range(100000)] workQueue.queue.clear() print('put data start -->', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) putFlag=True startIndex = 0 while putFlag: blank_count = workQueue.maxsize - workQueue.qsize() if blank_count > 0: endIndex = (startIndex + blank_count) if (startIndex + blank_count) < len(nameList) else len(nameList) queueLock.acquire() for x in range(startIndex, endIndex): workQueue.put(nameList[x]) if (x >= len(nameList) - 1): print('put ok,wait -->', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) putFlag = False startIndex = x + 1 print('wait process -->', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) queueLock.release() time.sleep(0.01) print('put data end -->', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) def main(): global exitFlag exitFlag = 0 global putFlag threadListName = ['Th-' + str(i) for i in range(100)] threads = [] # 创建线程 for index, item in enumerate(threadListName, 1): t = MyThread(index, item, workQueue) t.start() threads.append(t) # add_data = threading.Thread(target=addDataToQueue) # add_data.start() # threads.append(add_data) addDataToQueue() while not workQueue.empty(): pass exitFlag = 1 for t in threads: t.join() print('Exit All Thread') if __name__ == '__main__': queueLock = threading.Lock() workQueue = queue.Queue(100) main() 「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」 赞赏 × 梦白沙 (๑>ڡ<)☆谢谢老板~ 1元 2元 5元 10元 50元 任意金额 2元 使用微信扫描二维码完成支付 版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。 Python 2022-04-23 评论 294 次浏览