python使用Queue实现优先级队列,pythonqueue,使用Queue.Queu
python使用Queue实现优先级队列,pythonqueue,使用Queue.Queu
使用Queue.Queue实现的线程安全的优先级队列:
import Queueclass PriorityQueue(Queue.Queue): def _put(self, item): data, priority = item self._insort_right((priority, data)) def _get(self): return self.queue.pop(0)[1] def _insort_right(self, x): """Insert item x in list, and keep it sorted assuming a is sorted. If x is already in list, insert it to the right of the rightmost x. """ a = self.queue lo = 0 hi = len(a) while lo < hi: mid = (lo+hi)/2 if x[0] < a[mid][0]: hi = mid else: lo = mid+1 a.insert(lo, x)def test(): pq = PriorityQueue() pq.put(('b', 1)) pq.put(('a', 1)) pq.put(('c', 1)) pq.put(('z', 0)) pq.put(('d', 2)) while not pq.empty(): print pq.get(), test() # prints z b a c d
相关内容
- python循环监控远程端口的代码,python端口代码,在ip.tx
- python基础教程代码分享,python基础教程分享,people = {
- Python 扫描IP段 指定端口是否开放,pythonip,Python 扫描I
- 将十进制的数转换为任意进制,十进制数转换进制,#c
- python实现ssh批量登录并执行命令,pythonssh执行命令,局域
- 自己写的简单的类似minecraft的游戏,类似minecraft游戏
- 遍历制定目录下所有文件,制定目录,#_*_coding:u
- python获得本机机器名,python获得机器,import sys,
- 自动识别现接serial端口名称,识别serial端口名称,#!/us
- python通过wxPython打开并播放wav文件,,''' wx_lib_f
评论关闭