subscriber#

class litestar.channels.subscriber.Subscriber#

Bases: object

A wrapper around a stream of events published to subscribed channels

__init__(plugin: ChannelsPlugin, max_backlog: int | None = None, backlog_strategy: BacklogStrategy = 'backoff') None#
put_nowait(item: bytes | None) bool#

Put an item in the subscriber’s stream without waiting

async iter_events() AsyncGenerator[bytes, None]#

Iterate over the stream of events. If no items are available, block until one becomes available

run_in_background(on_event: Callable[[bytes], Awaitable[Any]], join: bool = True) AsyncGenerator[None, None]#

Start a task in the background that sends events from the subscriber’s stream to socket as they become available. On exit, it will prevent the stream from accepting new events and wait until the currently enqueued ones are processed. Should the context be left with an exception, the task will be cancelled immediately.

Parameters:
  • on_event – Callback to invoke with the event data for every event

  • join – If True, wait for all items in the stream to be processed before stopping the worker. Note that an error occurring within the context will always lead to the immediate cancellation of the worker

property is_running: bool#

Return whether a sending task is currently running

async stop(join: bool = False) None#

Stop a task was previously started with run_in_background(). If the task is not yet done it will be cancelled and awaited

Parameters:

join – If True wait for all items to be processed before stopping the task