Repository: alexander-akhmetov/python-telegram

Issue:
Remove time.sleep from AsyncResult

Discussion:

Patch:
diff --git a/telegram/client.py b/telegram/client.py
index 4e232bd..a1afdc6 100644
--- a/telegram/client.py
+++ b/telegram/client.py
@@ -221,8 +221,8 @@ class Telegram:
 
     def get_message(
         self,
-        chat_id,
-        message_id,
+        chat_id: int,
+        message_id: int,
     ) -> AsyncResult:
         """
         Return a message via its message_id
diff --git a/telegram/utils.py b/telegram/utils.py
index 97a65c0..879b480 100644
--- a/telegram/utils.py
+++ b/telegram/utils.py
@@ -1,5 +1,5 @@
-import time
 import uuid
+import threading
 from typing import TYPE_CHECKING, Any, Dict, Optional
 
 if TYPE_CHECKING:
@@ -25,6 +25,7 @@ class AsyncResult:
         self.error = False
         self.error_info: Optional[Dict[Any, Any]] = None
         self.update: Optional[Dict[Any, Any]] = None
+        self._ready = threading.Event()
 
     def __str__(self) -> str:
         return f'AsyncResult <{self.id}>'
@@ -33,15 +34,11 @@ class AsyncResult:
         """
         Blocking method to wait for the result
         """
-        started_at = time.time()
-        while True:
-            if self.update or self.error:
-                if raise_exc and self.error:
-                    raise RuntimeError(f'Telegram error: {self.error_info}')
-                return
-            time.sleep(0.01)
-            if timeout and time.time() - started_at > timeout:
-                raise TimeoutError()
+        result = self._ready.wait(timeout=timeout)
+        if result is False:
+            raise TimeoutError()
+        if raise_exc and self.error:
+            raise RuntimeError(f'Telegram error: {self.error_info}')
 
     def parse_update(self, update: Dict[Any, Any]) -> None:
         if update.get('@type') == 'ok':
@@ -51,3 +48,5 @@ class AsyncResult:
             self.error_info = update
         else:
             self.update = update
+
+        self._ready.set()


Test patch:
diff --git a/tests/test_telegram_methods.py b/tests/test_telegram_methods.py
index ada7e9e..d66f8d2 100644
--- a/tests/test_telegram_methods.py
+++ b/tests/test_telegram_methods.py
@@ -301,6 +301,7 @@ class TestTelegram__login:
             result = AsyncResult(client=telegram)
 
             result.update = data
+            result._ready.set()
             result.id = request_id
 
             return result
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 51ea333..265ef86 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -68,12 +68,14 @@ class TestAsyncResult(object):
     def test_wait_with_update(self):
         ar = AsyncResult(client=None)
         ar.update = '123'
+        ar._ready.set()
         ar.wait(timeout=0.01)
 
     def test_wait_with_error_and_raise_exc(self):
         ar = AsyncResult(client=None)
         ar.error = True
         ar.error_info = 'some_error'
+        ar._ready.set()
         try:
             ar.wait(timeout=0.1, raise_exc=True)
             raised = False
@@ -86,4 +88,5 @@ class TestAsyncResult(object):
         ar = AsyncResult(client=None)
         ar.error = True
         ar.error_info = 'some_error'
+        ar._ready.set()
         ar.wait(timeout=0.01)