Skip to content

Commit

Permalink
Attempt at not calling .connect for every file chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Oct 14, 2017
1 parent 9907d76 commit 280a700
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions telethon/telegram_bare_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,23 +416,33 @@ def _get_cdn_client(self, cdn_redirect):

# 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.
The invoke will be retried up to 'retries' times before raising
ValueError().
If 'sender' is not None, it will override the sender to be used.
"""
if not all(isinstance(x, TLObject) and
x.content_related for x in requests):
raise ValueError('You can only invoke requests, not types!')

# Determine the sender to be used (main or a new connection)
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()
sender = sender or self._get_sender(on_main_thread=on_main_thread)

# We should call receive from this thread if there's no background
# thread reading or if the server disconnected us and we're trying
Expand Down Expand Up @@ -686,6 +696,10 @@ def download_file(self,

# The used client will change if FileMigrateError occurs
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

try:
Expand All @@ -697,7 +711,7 @@ def download_file(self,
else:
result = client(GetFileRequest(
input_location, offset, part_size
))
), sender=sender)

if isinstance(result, FileCdnRedirect):
cdn_decrypter, result = \
Expand All @@ -706,7 +720,11 @@ def download_file(self,
)

except FileMigrateError as e:
if sender != self._sender:
sender.disconnect()
client = self._get_exported_client(e.new_dc)
# Client connected on this thread -> uses the right sender
sender = None
continue

offset += part_size
Expand All @@ -721,6 +739,8 @@ def download_file(self,
if progress_callback:
progress_callback(f.tell(), file_size)
finally:
if sender != self._sender:
sender.disconnect()
if client != self:
client.disconnect()

Expand Down

0 comments on commit 280a700

Please sign in to comment.