telegram.ext.MessageQueue

class telegram.ext.MessageQueue(all_burst_limit: int = 30, all_time_limit_ms: int = 1000, group_burst_limit: int = 20, group_time_limit_ms: int = 60000, exc_route: Callable[[Exception], None] = None, autostart: bool = True)

Bases: object

Implements callback processing with proper delays to avoid hitting Telegram’s message limits. Contains two DelayQueue, for group and for all messages, interconnected in delay chain. Callables are processed through group DelayQueue, then through all DelayQueue for group-type messages. For non-group messages, only the all DelayQueue is used.

Parameters:
  • all_burst_limit (int, optional) – Number of maximum all-type callbacks to process per time-window defined by all_time_limit_ms. Defaults to 30.
  • all_time_limit_ms (int, optional) – Defines width of all-type time-window used when each processing limit is calculated. Defaults to 1000 ms.
  • group_burst_limit (int, optional) – Number of maximum group-type callbacks to process per time-window defined by group_time_limit_ms. Defaults to 20.
  • group_time_limit_ms (int, optional) – Defines width of group-type time-window used when each processing limit is calculated. Defaults to 60000 ms.
  • exc_route (callable, optional) – A callable, accepting one positional argument; used to route exceptions from processor threads to main thread; is called on Exception subclass exceptions. If not provided, exceptions are routed through dummy handler, which re-raises them.
  • autostart (bool, optional) – If True, processors are started immediately after object’s creation; if False, should be started manually by start method. Defaults to True.
__call__(promise: Callable, is_group_msg: bool = False) → Callable

Processes callables in throughput-limiting queues to avoid hitting limits (specified with burst_limit and time_limit.

Parameters:
  • promise (callable) – Mainly the telegram.utils.promise.Promise (see Notes for other callables), that is processed in delay queues.
  • is_group_msg (bool, optional) – Defines whether promise would be processed in group*+*all* DelayQueue``s (if set to :obj:`True`), or only through *all* ``DelayQueue (if set to False), resulting in needed delays to avoid hitting specified limits. Defaults to False.

Note

Method is designed to accept telegram.utils.promise.Promise as promise argument, but other callables could be used too. For example, lambdas or simple functions could be used to wrap original func to be called with needed args. In that case, be sure that either wrapper func does not raise outside exceptions or the proper exc_route handler is provided.

Returns:Used as promise argument.
Return type:callable
__init__(all_burst_limit: int = 30, all_time_limit_ms: int = 1000, group_burst_limit: int = 20, group_time_limit_ms: int = 60000, exc_route: Callable[[Exception], None] = None, autostart: bool = True)

Initialize self. See help(type(self)) for accurate signature.

__weakref__

list of weak references to the object (if defined)

start() → None

Method is used to manually start the MessageQueue processing.

stop(timeout: float = None) → None

Used to gently stop processor and shutdown its thread.

Parameters:timeout (float) – Indicates maximum time to wait for processor to stop and its thread to exit. If timeout exceeds and processor has not stopped, method silently returns. is_alive could be used afterwards to check the actual status. timeout set to None, blocks until processor is shut down. Defaults to None.