77"""
88from __future__ import division
99
10+ import heapq
1011import sys
1112
12- try :
13- import Queue as queue
14- except ImportError : # Python 3:
15- import queue
16-
1713
1814PY3 = sys .version_info [0 ] == 3
1915
@@ -59,7 +55,7 @@ def __init__(self, stream_id, weight=16):
5955 self .weight = weight
6056 self .children = []
6157 self .parent = None
62- self .child_queue = queue . PriorityQueue ()
58+ self .child_queue = []
6359 self .active = True
6460 self .last_weight = 0
6561 self ._deficit = 0
@@ -72,7 +68,7 @@ def add_child(self, child):
7268 """
7369 child .parent = self
7470 self .children .append (child )
75- self .child_queue . put ( (self .last_weight , child ))
71+ heapq . heappush ( self .child_queue , (self .last_weight , child ))
7672
7773 def add_child_exclusive (self , child ):
7874 """
@@ -82,7 +78,7 @@ def add_child_exclusive(self, child):
8278 """
8379 old_children = self .children
8480 self .children = []
85- self .child_queue = queue . PriorityQueue ()
81+ self .child_queue = []
8682 self .last_weight = 0
8783 self .add_child (child )
8884
@@ -105,14 +101,14 @@ def remove_child(self, child, strip_children=True):
105101 # it in the old one
106102 self .children .remove (child )
107103
108- new_queue = queue . PriorityQueue ()
104+ new_queue = []
109105
110- while not self .child_queue . empty () :
111- level , stream = self .child_queue . get ( )
106+ while self .child_queue :
107+ level , stream = heapq . heappop ( self .child_queue )
112108 if stream == child :
113109 continue
114110
115- new_queue . put ( (level , stream ))
111+ heapq . heappush ( new_queue , (level , stream ))
116112
117113 self .child_queue = new_queue
118114
@@ -137,7 +133,7 @@ def schedule(self):
137133 try :
138134 while next_stream is None :
139135 # If the queue is empty, immediately fail.
140- val = self .child_queue . get ( block = False )
136+ val = heapq . heappop ( self .child_queue )
141137 popped_streams .append (val )
142138 level , child = val
143139
@@ -148,14 +144,14 @@ def schedule(self):
148144 # suitable children.
149145 try :
150146 next_stream = child .schedule ()
151- except queue . Empty :
147+ except IndexError :
152148 continue
153149 finally :
154150 for level , child in popped_streams :
155151 self .last_weight = level
156152 level += (256 + child ._deficit ) // child .weight
157153 child ._deficit = (256 + child ._deficit ) % child .weight
158- self .child_queue . put ( (level , child ))
154+ heapq . heappush ( self .child_queue , (level , child ))
159155
160156 return next_stream
161157
@@ -369,7 +365,7 @@ def __iter__(self): # pragma: no cover
369365 def __next__ (self ): # pragma: no cover
370366 try :
371367 return self ._root_stream .schedule ()
372- except queue . Empty :
368+ except IndexError :
373369 raise DeadlockError ("No unblocked streams to schedule." )
374370
375371 def next (self ): # pragma: no cover
0 commit comments