@@ -108,9 +108,12 @@ async def stream_range_as_completed(self, file_index, start, end):
108108 piece_size = self ._torrent_info .piece_length ()
109109 log .info ("Streaming torrent from piece %d to %d (bytes: %d -> %d): %s" ,
110110 first_piece .piece , final_piece .piece , start , end , self .name )
111+ self .prioritize (file_index , start , end )
112+ await self .resume ()
111113 for piece_index in range (first_piece .piece , final_piece .piece + 1 ):
112114 while not self ._handle .have_piece (piece_index ):
113115 log .info ("Waiting for piece %d: %s" , piece_index , self .name )
116+ self ._handle .set_piece_deadline (piece_index , 0 )
114117 await asyncio .sleep (0.2 )
115118 log .info ("Streaming piece offset %d / %d for torrent %s" , piece_index , final_piece .piece , self .name )
116119 yield piece_size - start_piece_offset
@@ -128,31 +131,38 @@ def _show_status(self):
128131 self .metadata_completed .set ()
129132 self ._torrent_info = self ._handle .torrent_file ()
130133 log .info ("Metadata completed for btih:%s - %s" , status .info_hash , self .name )
134+ # prioritize first 2mb
135+ self .prioritize (self .largest_file_index , 0 , 2 * 1024 * 1024 )
131136 self ._base_path = status .save_path
132137 first_piece = self .torrent_file .piece_index_at_file (self .largest_file_index )
133138 if not self .started .is_set ():
134139 if self ._handle .have_piece (first_piece ):
140+ log .debug ("Got first piece, set started - %s" , self .name )
135141 self .started .set ()
136- else :
137- # prioritize it
138- self ._handle .set_piece_deadline (first_piece , 100 )
139- prios = self ._handle .piece_priorities ()
140- prios [first_piece ] = 7
141- self ._handle .prioritize_pieces (prios )
142- if not status .is_seeding :
143- log .debug ('%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d seeds: %d) %s - %s' ,
144- status .progress * 100 , status .download_rate / 1000 , status .upload_rate / 1000 ,
145- status .num_peers , status .num_seeds , status .state , status .save_path )
146- elif not self .finished .is_set ():
142+ log .debug ('%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d seeds: %d) %s - %s' ,
143+ status .progress * 100 , status .download_rate / 1000 , status .upload_rate / 1000 ,
144+ status .num_peers , status .num_seeds , status .state , status .save_path )
145+ if (status .is_finished or status .is_seeding ) and not self .finished .is_set ():
147146 self .finished .set ()
148147 log .info ("Torrent finished: %s" , self .name )
149148
149+ def prioritize (self , file_index , start , end , cleanup = False ):
150+ first_piece , last_piece = self .byte_range_to_piece_range (file_index , start , end )
151+ priorities = self ._handle .get_piece_priorities ()
152+ priorities = [0 if cleanup else 1 for _ in priorities ]
153+ self ._handle .clear_piece_deadlines ()
154+ for idx , piece_number in enumerate (range (first_piece .piece , last_piece .piece + 1 )):
155+ priorities [piece_number ] = 7 - idx if 0 <= idx <= 6 else 1
156+ self ._handle .set_piece_deadline (piece_number , idx )
157+ log .debug ("Prioritizing pieces for %s: %s" , self .name , priorities )
158+ self ._handle .prioritize_pieces (priorities )
159+
150160 async def status_loop (self ):
151161 while True :
152162 self ._show_status ()
153163 if self .finished .is_set ():
154164 break
155- await asyncio .sleep (0.1 , loop = self ._loop )
165+ await asyncio .sleep (0.5 , loop = self ._loop )
156166
157167 async def pause (self ):
158168 await self ._loop .run_in_executor (
0 commit comments