HTTP/2 Priority Tree

2.0.0 · maintenance · verified Thu Apr 09

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

Install

Imports

Quickstart

Initialize a `PriorityTree`, insert streams with various dependencies and weights, then iterate using `next_stream_to_send()` to determine the optimal sending order. Demonstrates how to block and unblock streams to change their active status within the tree.

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}")

view raw JSON →