From ae88c851bbc98785abb829a748d68803f3a44c37 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 5 Sep 2020 12:44:48 +0200 Subject: [PATCH] Fix sync callback progress not working properly for downloads - Reduce duplicated code - Fixes #484 --- pyrogram/client.py | 30 ++++++++++++++------------ pyrogram/methods/advanced/save_file.py | 18 ++++++++-------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/pyrogram/client.py b/pyrogram/client.py index e7683c26..2577745a 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -949,7 +949,8 @@ class Client(Methods, Scaffold): offset += limit if progress: - await progress( + func = functools.partial( + progress, min(offset, file_size) if file_size != 0 else offset, @@ -957,6 +958,11 @@ class Client(Methods, Scaffold): *progress_args ) + if inspect.iscoroutinefunction(progress): + await func() + else: + await self.loop.run_in_executor(self.executor, func) + r = await session.send( raw.functions.upload.GetFile( location=location, @@ -1035,20 +1041,16 @@ class Client(Methods, Scaffold): offset += limit if progress: - if inspect.iscoroutinefunction(progress): - await progress( - min(offset, file_size) if file_size != 0 else offset, - file_size, - *progress_args - ) - else: - func = functools.partial( - progress, - min(offset, file_size) if file_size != 0 else offset, - file_size, - *progress_args - ) + func = functools.partial( + progress, + min(offset, file_size) if file_size != 0 else offset, + file_size, + *progress_args + ) + if inspect.iscoroutinefunction(progress): + await func() + else: await self.loop.run_in_executor(self.executor, func) if len(chunk) < limit: diff --git a/pyrogram/methods/advanced/save_file.py b/pyrogram/methods/advanced/save_file.py index 9d770733..e9727951 100644 --- a/pyrogram/methods/advanced/save_file.py +++ b/pyrogram/methods/advanced/save_file.py @@ -183,16 +183,16 @@ class SaveFile(Scaffold): file_part += 1 if progress: - if inspect.iscoroutinefunction(progress): - await progress(min(file_part * part_size, file_size), file_size, *progress_args) - else: - func = functools.partial( - progress, - min(file_part * part_size, file_size), - file_size, - *progress_args - ) + func = functools.partial( + progress, + min(file_part * part_size, file_size), + file_size, + *progress_args + ) + if inspect.iscoroutinefunction(progress): + await func() + else: await self.loop.run_in_executor(self.executor, func) except StopTransmission: raise