The core of the command dispatcher.


You do not have to add this middleware yourself, the Builder will add it for you.


The dispatch middleware actually handles the dispatching of requests to their handlers. It should be the last middleware in the chain.


You do not have to add this middleware yourself, the Builder will add it for you.

Instead you setup the middleware using with_locator() and with_factory() to provide its dependencies.


We add a Dispatch context instance for each handler executed. It will contain the result of the function call and the name of the handler.

result = message[banshee.Dispatch].result
name = message[banshee.Dispatch].name


class banshee.Dispatch(name, result)

Bases: object

Dispatch context.

Marks a message as having had its request processed by a specific handler and contains the result of that execution.

  • name (str) – handler name

  • result (Any | None) – result of handler

name: str

handler name

result: Any | None

result of handler

class banshee.DispatchMiddleware(locator, factory)

Bases: Middleware

Dispatch middleware.

Implements the command dispatcher design pattern to route requests to their configured handlers.

Uses the pre-configured HandlerLocator to retrieve a all associated HandlerReference instances, before passing the references one by one to a HandlerFactory in order to get and Handler instance to call.

A Dispatch context will be added to the message for each successful handler.

  • locator (HandlerLocator) – locator to lookup associated handlers for a message

  • factory (HandlerFactory) – factory to instantiate a concrete handler from a reference

async __call__(message, handle)

Handle message.

Dispatch the request to all associated handlers and store the results.


processed message


banshee.errors.DispatchError – when one or more handlers fails

Return type:


exception banshee.DispatchError(message, exceptions)

Dispatch error.

There was an error while handling a request.