@@ -74,9 +74,12 @@ async def stream_range_as_completed(self, file_index, start, end):
7474 piece_size = self ._torrent_info .piece_length ()
7575 log .info ("Streaming torrent from piece %d to %d (bytes: %d -> %d): %s" ,
7676 first_piece .piece , final_piece .piece , start , end , self .name )
77+ self .prioritize (file_index , start , end )
78+ await self .resume ()
7779 for piece_index in range (first_piece .piece , final_piece .piece + 1 ):
7880 while not self ._handle .have_piece (piece_index ):
7981 log .info ("Waiting for piece %d: %s" , piece_index , self .name )
82+ self ._handle .set_piece_deadline (piece_index , 0 )
8083 await asyncio .sleep (0.2 )
8184 log .info ("Streaming piece offset %d / %d for torrent %s" , piece_index , final_piece .piece , self .name )
8285 yield piece_size - start_piece_offset
@@ -94,31 +97,38 @@ def _show_status(self):
9497 self .metadata_completed .set ()
9598 self ._torrent_info = self ._handle .torrent_file ()
9699 log .info ("Metadata completed for btih:%s - %s" , status .info_hash , self .name )
100+ # prioritize first 2mb
101+ self .prioritize (self .largest_file_index , 0 , 2 * 1024 * 1024 )
97102 self ._base_path = status .save_path
98103 first_piece = self .torrent_file .piece_index_at_file (self .largest_file_index )
99104 if not self .started .is_set ():
100105 if self ._handle .have_piece (first_piece ):
106+ log .debug ("Got first piece, set started - %s" , self .name )
101107 self .started .set ()
102- else :
103- # prioritize it
104- self ._handle .set_piece_deadline (first_piece , 100 )
105- prios = self ._handle .piece_priorities ()
106- prios [first_piece ] = 7
107- self ._handle .prioritize_pieces (prios )
108- if not status .is_seeding :
109- log .debug ('%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d seeds: %d) %s - %s' ,
110- status .progress * 100 , status .download_rate / 1000 , status .upload_rate / 1000 ,
111- status .num_peers , status .num_seeds , status .state , status .save_path )
112- elif not self .finished .is_set ():
108+ log .debug ('%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d seeds: %d) %s - %s' ,
109+ status .progress * 100 , status .download_rate / 1000 , status .upload_rate / 1000 ,
110+ status .num_peers , status .num_seeds , status .state , status .save_path )
111+ if (status .is_finished or status .is_seeding ) and not self .finished .is_set ():
113112 self .finished .set ()
114113 log .info ("Torrent finished: %s" , self .name )
115114
115+ def prioritize (self , file_index , start , end , cleanup = False ):
116+ first_piece , last_piece = self .byte_range_to_piece_range (file_index , start , end )
117+ priorities = self ._handle .get_piece_priorities ()
118+ priorities = [0 if cleanup else 1 for _ in priorities ]
119+ self ._handle .clear_piece_deadlines ()
120+ for idx , piece_number in enumerate (range (first_piece .piece , last_piece .piece + 1 )):
121+ priorities [piece_number ] = 7 - idx if 0 <= idx <= 6 else 1
122+ self ._handle .set_piece_deadline (piece_number , idx )
123+ log .debug ("Prioritizing pieces for %s: %s" , self .name , priorities )
124+ self ._handle .prioritize_pieces (priorities )
125+
116126 async def status_loop (self ):
117127 while True :
118128 self ._show_status ()
119129 if self .finished .is_set ():
120130 break
121- await asyncio .sleep (0.1 , loop = self ._loop )
131+ await asyncio .sleep (0.5 , loop = self ._loop )
122132
123133 async def pause (self ):
124134 await self ._loop .run_in_executor (
0 commit comments