https://github.com/pykit3/k3priorityqueue
priorityQueue is a queue with priority support
https://github.com/pykit3/k3priorityqueue
priorityqueue python
Last synced: 12 days ago
JSON representation
priorityQueue is a queue with priority support
- Host: GitHub
- URL: https://github.com/pykit3/k3priorityqueue
- Owner: pykit3
- License: mit
- Created: 2021-07-22T03:23:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-08-28T12:37:58.000Z (4 months ago)
- Last Synced: 2025-10-27T04:34:57.861Z (2 months ago)
- Topics: priorityqueue, python
- Language: Python
- Homepage: https://blog.openacid.com
- Size: 34.2 KB
- Stars: 0
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# k3priorityqueue
[](https://github.com/pykit3/k3priorityqueue/actions/workflows/python-package.yml)
[](https://travis-ci.com/pykit3/k3priorityqueue)
[](https://k3priorityqueue.readthedocs.io/en/stable/?badge=stable)
[](https://pypi.org/project/k3priorityqueue)
priorityQueue is a queue with priority support
k3priorityqueue is a component of [pykit3] project: a python3 toolkit set.
PriorityQueue is a queue with priority support:
The numbers of items it pops from each producer matches exactly the ratio of their priority:
If the priorities of 3 producer A, B and C are 1, 3 and 7, and it runs long
enough, it is expected that the number of items popped from A, B and C are
1:3:7.
import k3priorityqueue
producers = (
# id, priority, iterable
(1, 1, [1] * 10),
(2, 2, [2] * 10),
(3, 3, [3] * 10),
)
pq = k3priorityqueue.PriorityQueue()
for pid, prio, itr in producers:
pq.add_producer(pid, prio, itr)
count = {}
for _ in range(12):
val = pq.get()
count[val] = count.get(val, 0) + 1
print(val)
print('respect priority ratio: counts:', repr(count))
while True:
try:
val = pq.get()
except k3priorityqueue.Empty as e:
break
count[val] = count.get(val, 0) + 1
print(val)
print('consumed all: counts:', repr(count))
# Install
```
pip install k3priorityqueue
```
# Synopsis
```python
import k3priorityqueue
producers = (
# id, priority, iterable
(1, 1, [1] * 10),
(2, 2, [2] * 10),
(3, 3, [3] * 10),
)
pq = k3priorityqueue.PriorityQueue()
for pid, prio, itr in producers:
pq.add_producer(pid, prio, itr)
count = {}
for _ in range(12):
val = pq.get()
count[val] = count.get(val, 0) + 1
print(val)
print('respect priority ratio: counts:', repr(count))
while True:
try:
val = pq.get()
except k3priorityqueue.Empty as e:
break
count[val] = count.get(val, 0) + 1
print(val)
print('consumed all: counts:', repr(count))
```
# Author
Zhang Yanpo (张炎泼)
# Copyright and License
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼)
[pykit3]: https://github.com/pykit3