2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Allow user-defined values for retries and timeout

This commit is contained in:
Dan 2018-06-24 17:47:29 +02:00
parent 50ab50eb52
commit 69d1432251
3 changed files with 15 additions and 8 deletions

View File

@ -817,7 +817,7 @@ class Client(Methods, BaseClient):
log.debug("{} stopped".format(name)) log.debug("{} stopped".format(name))
def send(self, data: Object): def send(self, data: Object, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT):
"""Use this method to send Raw Function queries. """Use this method to send Raw Function queries.
This method makes possible to manually call every single Telegram API method in a low-level manner. This method makes possible to manually call every single Telegram API method in a low-level manner.
@ -828,13 +828,19 @@ class Client(Methods, BaseClient):
data (``Object``): data (``Object``):
The API Scheme function filled with proper arguments. The API Scheme function filled with proper arguments.
retries (``int``):
Number of retries.
timeout (``float``):
Timeout in seconds.
Raises: Raises:
:class:`Error <pyrogram.Error>` :class:`Error <pyrogram.Error>`
""" """
if not self.is_started: if not self.is_started:
raise ConnectionError("Client has not been started") raise ConnectionError("Client has not been started")
r = self.session.send(data) r = self.session.send(data, retries, timeout)
self.fetch_peers(getattr(r, "users", [])) self.fetch_peers(getattr(r, "users", []))
self.fetch_peers(getattr(r, "chats", [])) self.fetch_peers(getattr(r, "chats", []))

View File

@ -23,6 +23,7 @@ from threading import Lock
from ..style import Markdown, HTML from ..style import Markdown, HTML
from ...api.core import Object from ...api.core import Object
from ...session.internals import MsgId from ...session.internals import MsgId
from ...session import Session
class BaseClient: class BaseClient:
@ -77,7 +78,7 @@ class BaseClient:
self.disconnect_handler = None self.disconnect_handler = None
def send(self, data: Object): def send(self, data: Object, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT):
pass pass
def resolve_peer(self, peer_id: int or str): def resolve_peer(self, peer_id: int or str):

View File

@ -391,7 +391,7 @@ class Session:
log.debug("RecvThread stopped") log.debug("RecvThread stopped")
def _send(self, data: Object, wait_response: bool = True): def _send(self, data: Object, wait_response: bool = True, timeout: float = WAIT_TIMEOUT):
message = self.msg_factory(data) message = self.msg_factory(data)
msg_id = message.msg_id msg_id = message.msg_id
@ -407,7 +407,7 @@ class Session:
raise e raise e
if wait_response: if wait_response:
self.results[msg_id].event.wait(self.WAIT_TIMEOUT) self.results[msg_id].event.wait(timeout)
result = self.results.pop(msg_id).value result = self.results.pop(msg_id).value
if result is None: if result is None:
@ -422,11 +422,11 @@ class Session:
else: else:
return result return result
def send(self, data: Object, retries: int = MAX_RETRIES): def send(self, data: Object, retries: int = MAX_RETRIES, timeout: float = WAIT_TIMEOUT):
self.is_connected.wait(self.WAIT_TIMEOUT) self.is_connected.wait(self.WAIT_TIMEOUT)
try: try:
return self._send(data) return self._send(data, timeout=timeout)
except (OSError, TimeoutError, InternalServerError) as e: except (OSError, TimeoutError, InternalServerError) as e:
if retries == 0: if retries == 0:
raise e from None raise e from None
@ -437,4 +437,4 @@ class Session:
datetime.now(), type(data))) datetime.now(), type(data)))
time.sleep(0.5) time.sleep(0.5)
return self.send(data, retries - 1) return self.send(data, retries - 1, timeout)