HTTP/2 Priority Tree
Priority is a pure-Python implementation of the HTTP/2 priority logic, as described in RFC 7540 Section 5.3 (Stream Priority). It allows clients to express preferences for how servers allocate resources across concurrent HTTP/2 requests by managing a priority tree. The library uses a variant of the implementation from the H2O project. The current version, 2.0.0, was released in June 2021, indicating a stable project in maintenance mode, part of the broader python-hyper ecosystem for HTTP/2 related libraries.
Warnings
- gotcha HTTP/2 stream prioritization is a suggestion to the server, not a strict command. Servers and intermediate network elements (like proxies or CDNs) may not fully respect client-side priority requests due to their own implementation details, buffering, or policy, potentially leading to resource allocation that doesn't match client expectations.
- gotcha The `PriorityTree`'s effectiveness relies on accurate and timely updates of stream states. Failing to call methods like `block(stream_id)` when a stream is unable to send data, or `unblock(stream_id)` when it becomes active again, can lead to suboptimal or stalled resource allocation.
Install
-
pip install priority
Imports
- PriorityTree
from priority import PriorityTree
Quickstart
import priority
p = priority.PriorityTree()
p.insert_stream(stream_id=1)
p.insert_stream(stream_id=3)
p.insert_stream(stream_id=5, depends_on=1)
p.insert_stream(stream_id=7, weight=32)
p.insert_stream(stream_id=9, depends_on=7, weight=8)
p.insert_stream(stream_id=11, depends_on=7, exclusive=True)
# Simulate sending data by iterating the tree
print("Initial serving order:")
for _ in range(10):
next_stream_id = p.next_stream_to_send()
if next_stream_id is None:
print(" No more active streams.")
break
print(f" Sending data for stream: {next_stream_id}")
# Simulate blocking a stream after it's been served once
if next_stream_id == 7:
p.block(7)
print(" Stream 7 blocked for demonstration.")
# Unblock a stream to see it re-enter the queue
p.unblock(7)
print("\nStream 7 unblocked. Continuing serving order:")
for _ in range(5):
next_stream_id = p.next_stream_to_send()
if next_stream_id is None:
print(" No more active streams.")
break
print(f" Sending data for stream: {next_stream_id}")