擅长:python、mysql、java
<p>传统的解决方案是在堆上存储(优先级,任务)元组:</p>
<pre><code>pq = [ ]
heappush(pq, (10, task1))
heappush(pq, (5, task2))
heappush(pq, (15, task3))
priority, task = heappop(pq)
</code></pre>
<p>只要没有两个任务具有相同的优先级,这就可以正常工作;否则,将比较任务本身(这在Python 3中可能根本不起作用)。</p>
<p>常规文档指导如何使用heapq实现优先级队列:</p>
<p><a href="http://docs.python.org/library/heapq.html#priority-queue-implementation-notes" rel="noreferrer">http://docs.python.org/library/heapq.html#priority-queue-implementation-notes</a></p>