zoukankan      html  css  js  c++  java
  • 线程优先级队列

    #Queue模块可以用来进行线程间的通信,让各个线程之间共享数据。
    #Python的Queue模块提供了同步、线程安全的队列类,包括FIFO(先入先出)队列Queue、LIFO(后入先出)队列LifoQueue和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用。可以使用队列实现线程间的同步。
    #Queue模块中的常用方法如下表:
    方法名
    描述
    qsize()
    返回队列的大小
    empty()
    如果队列为空,返回True,反之返回False
    full()
    如果队列满了,返回True,反之返回False
    full
    与MaxSize大小对应
    get([block[,timeout]])
    获取队列,timeout等待时间
    get_nowait()
    相当于Queue.get(False)
    put(timeout)
    写入队列,timeout等待时间
    put_nowait(item)
    相当于Queue.put(item,False)
    task_done()
    在完成一项工作后,函数向已经完成的队列
    发送一个信号
    join()
    实际上意味着等到队列为空,再执行别的操作
    #下面通过以下示例了解其中一些方法的使用。
     1 #!/usr/bin/python3
     2 #-*-coding:UTF-8-*-
     3 #queue
     4 
     5 import threading
     6 import queue
     7 from time import sleep
     8 
     9 class MyThread(threading.Thread):
    10     def __init__(self,threadID,name,q):
    11         threading.Thread.__init__(self)
    12         self.threadID=threadID
    13         self.name=name
    14         self.q=q
    15 
    16     def run(self):
    17         print("开启线程:"+self.name)
    18         process_data(self.name,self.q)
    19         print("退出线程:"+self.name)
    20 
    21 def process_data(threadName,q):
    22     while not exitFlag:
    23         queueLock.aquire()
    24         if not workQueue.empty():
    25             data=q.get()
    26             queueLock.release()
    27             print("%s processing %s "%(threadName,data))
    28 
    29         else:
    30             queueLock.release()
    31         sleep(1)
    32 
    33 def main():
    34     global exitFlag
    35     exitFlag=0
    36     threadList=["Thread-1","Thread-2","Thread-3"]
    37     nameList=["one","two","three","four","five"]
    38 
    39     threads=[]
    40     threadID=1
    41 
    42     #创建新线程
    43     for tName in threadList:
    44         thread=MyThread(threadID,tName,workQueue)
    45         thread.start()
    46         threads.append(thread)
    47         threadID+=1
    48 
    49     #填充队列
    50     queueLock.acquire()
    51     for word in nameList:
    52         workQueue.put(word)
    53     queueLock.release()
    54 
    55     #等待队列清空
    56     while not workQueue.empty():
    57         pass
    58 
    59     #通知线程是退出的时候了
    60     exitFlag=1
    61 
    62     #等待所有线程完成
    63     for t in threads:
    64         t.join()
    65     print("退出主线程")
    66 
    67 if __name__=="__main__":
    68     queueLock=threading.Lock()
    69     workQueue=queue.Queue(10)
    70     main()
    #执行结果如下:
     1 D:Pythonworkspacedatatime20180208>python 线程优先级队列.py
     2 开启线程:Thread-1
     3 开启线程:Thread-2
     4 开启线程:Thread-3
     5 Thread-3 processing one
     6 Thread-1 processing two
     7 Thread-2 processing three
     8 Thread-3 processing four
     9 Thread-1 processing five
    10 退出线程:Thread-2
    11 退出线程:Thread-3
    12 退出线程:Thread-1
    13 退出主线程
  • 相关阅读:
    分布式框架---Dubbox 简介
    MySql 多表查询
    MySql 增删改查
    redis
    spring security 自定义登录页面及从数据库查询账户登录
    java数据结构-
    Maven-
    有关多行相同数据,只显示在第一行的实现
    javaWEB的第一次MVC之旅
    JavaWeb中的 请求转发 和 重定向
  • 原文地址:https://www.cnblogs.com/DLHe/p/8433451.html
Copyright © 2011-2022 走看看