Skip to content
Back to top
View this page

# BaseUpdateProcessor

class telegram.ext.BaseUpdateProcessor(max_concurrent_updates)

  Bases: `contextlib.AbstractAsyncContextManager`, `ABC`

  An abstract base class for update processors. You can use this class to implement your own update processor.

  Instances of this class can be used as asyncio context managers, where

      async with processor:
          # code

  is roughly equivalent to

      try:
          await processor.initialize()
          # code
      finally:
          await processor.shutdown()

  See also

  `__aenter__()` and `__aexit__()`.

  See also

  Concurrency

  Use In

  `telegram.ext.ApplicationBuilder.concurrent_updates()`

  Available In

  `telegram.ext.Application.update_processor`

  Added in version 20.4.

  Parameters:

    max_concurrent_updates (`int`) – The maximum number of updates to be processed concurrently. If this number is exceeded, new updates will be queued until the number of currently processed updates decreases.

  Raises:

    ValueError – If `max_concurrent_updates` is a non-positive integer.

  async __aenter__()

    Asynchronous context manager which `initializes` the Processor.

    Returns:

      The initialized Processor instance.

    Raises:

      Exception – If an exception is raised during initialization, `shutdown()` is called in this case.

  async __aexit__(exc_type, exc_val, exc_tb)

    Asynchronous context manager which `shuts down` the Processor.

  property current_concurrent_updates

    The number of updates currently being processed.

    Caution

    This value is a snapshot of the current number of updates being processed. It may change immediately after being read.

    Added in version 21.11.

    Type:

      `int`

  abstractmethod async do_process_update(update, coroutine)

    Custom implementation of how to process an update. Must be implemented by a subclass.

    Warning

    This method will be called by `process_update()`. It should not be called manually.

    Parameters:

      - update (`object`) – The update to be processed.

      - coroutine (Awaitable) – The coroutine that will be awaited to process the update.

  abstractmethod async initialize()

    Initializes the processor so resources can be allocated. Must be implemented by a subclass.

    See also

    `shutdown()`

  property max_concurrent_updates

    The maximum number of updates that can be processed concurrently.

    Type:

      `int`

  final async process_update(update, coroutine)

    Calls `do_process_update()` with a semaphore to limit the number of concurrent updates.

    Parameters:

      - update (`object`) – The update to be processed.

      - coroutine (Awaitable) – The coroutine that will be awaited to process the update.

  abstractmethod async shutdown()

    Shutdown the processor so resources can be freed. Must be implemented by a subclass.

    See also

    `initialize()`