class telegram.ext.MessageQueue(all_burst_limit=30, all_time_limit_ms=1000, group_burst_limit=20, group_time_limit_ms=60000, exc_route=None, autostart=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.

  • 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, is_group_msg=False)

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

  • 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 ``True), or only through all DelayQueue (if set to False), resulting in needed delays to avoid hitting specified limits. Defaults to True.


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

list of weak references to the object (if defined)


Method is used to manually start the MessageQueue processing.


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.