mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-11-01 00:17:47 +03:00 
			
		
		
		
	Attempt at not calling .connect for every file chunk
This commit is contained in:
		
							parent
							
								
									9907d763a8
								
							
						
					
					
						commit
						280a700655
					
				|  | @ -416,11 +416,25 @@ class TelegramBareClient: | ||||||
| 
 | 
 | ||||||
|     # region Invoking Telegram requests |     # region Invoking Telegram requests | ||||||
| 
 | 
 | ||||||
|     def __call__(self, *requests, retries=5): |     def _get_sender(self, on_main_thread=None): | ||||||
|  |         """Gets the appropriated sender based on the current thread""" | ||||||
|  |         if on_main_thread is None: | ||||||
|  |             on_main_thread = threading.get_ident() == self._main_thread_ident | ||||||
|  | 
 | ||||||
|  |         if on_main_thread or self._on_read_thread(): | ||||||
|  |             sender = self._sender | ||||||
|  |         else: | ||||||
|  |             sender = self._sender.clone() | ||||||
|  |             sender.connect() | ||||||
|  |         return sender | ||||||
|  | 
 | ||||||
|  |     def __call__(self, *requests, retries=5, sender=None): | ||||||
|         """Invokes (sends) a MTProtoRequest and returns (receives) its result. |         """Invokes (sends) a MTProtoRequest and returns (receives) its result. | ||||||
| 
 | 
 | ||||||
|            The invoke will be retried up to 'retries' times before raising |            The invoke will be retried up to 'retries' times before raising | ||||||
|            ValueError(). |            ValueError(). | ||||||
|  | 
 | ||||||
|  |            If 'sender' is not None, it will override the sender to be used. | ||||||
|         """ |         """ | ||||||
|         if not all(isinstance(x, TLObject) and |         if not all(isinstance(x, TLObject) and | ||||||
|                    x.content_related for x in requests): |                    x.content_related for x in requests): | ||||||
|  | @ -428,11 +442,7 @@ class TelegramBareClient: | ||||||
| 
 | 
 | ||||||
|         # Determine the sender to be used (main or a new connection) |         # Determine the sender to be used (main or a new connection) | ||||||
|         on_main_thread = threading.get_ident() == self._main_thread_ident |         on_main_thread = threading.get_ident() == self._main_thread_ident | ||||||
|         if on_main_thread or self._on_read_thread(): |         sender = sender or self._get_sender(on_main_thread=on_main_thread) | ||||||
|             sender = self._sender |  | ||||||
|         else: |  | ||||||
|             sender = self._sender.clone() |  | ||||||
|             sender.connect() |  | ||||||
| 
 | 
 | ||||||
|         # We should call receive from this thread if there's no background |         # We should call receive from this thread if there's no background | ||||||
|         # thread reading or if the server disconnected us and we're trying |         # thread reading or if the server disconnected us and we're trying | ||||||
|  | @ -686,6 +696,10 @@ class TelegramBareClient: | ||||||
| 
 | 
 | ||||||
|         # The used client will change if FileMigrateError occurs |         # The used client will change if FileMigrateError occurs | ||||||
|         client = self |         client = self | ||||||
|  |         # TODO Keeping just another variable for a sender feels messy, improve. | ||||||
|  |         # This is done not to call .connect() for every single piece of the | ||||||
|  |         # file we'll be trying to download, if we were from another thread. | ||||||
|  |         sender = self._get_sender() | ||||||
|         cdn_decrypter = None |         cdn_decrypter = None | ||||||
| 
 | 
 | ||||||
|         try: |         try: | ||||||
|  | @ -697,7 +711,7 @@ class TelegramBareClient: | ||||||
|                     else: |                     else: | ||||||
|                         result = client(GetFileRequest( |                         result = client(GetFileRequest( | ||||||
|                             input_location, offset, part_size |                             input_location, offset, part_size | ||||||
|                         )) |                         ), sender=sender) | ||||||
| 
 | 
 | ||||||
|                         if isinstance(result, FileCdnRedirect): |                         if isinstance(result, FileCdnRedirect): | ||||||
|                             cdn_decrypter, result = \ |                             cdn_decrypter, result = \ | ||||||
|  | @ -706,7 +720,11 @@ class TelegramBareClient: | ||||||
|                                 ) |                                 ) | ||||||
| 
 | 
 | ||||||
|                 except FileMigrateError as e: |                 except FileMigrateError as e: | ||||||
|  |                     if sender != self._sender: | ||||||
|  |                         sender.disconnect() | ||||||
|                     client = self._get_exported_client(e.new_dc) |                     client = self._get_exported_client(e.new_dc) | ||||||
|  |                     # Client connected on this thread -> uses the right sender | ||||||
|  |                     sender = None | ||||||
|                     continue |                     continue | ||||||
| 
 | 
 | ||||||
|                 offset += part_size |                 offset += part_size | ||||||
|  | @ -721,6 +739,8 @@ class TelegramBareClient: | ||||||
|                 if progress_callback: |                 if progress_callback: | ||||||
|                     progress_callback(f.tell(), file_size) |                     progress_callback(f.tell(), file_size) | ||||||
|         finally: |         finally: | ||||||
|  |             if sender != self._sender: | ||||||
|  |                 sender.disconnect() | ||||||
|             if client != self: |             if client != self: | ||||||
|                 client.disconnect() |                 client.disconnect() | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user