Handle After

Handle requests after the current handler.

Note

Middleware uses ContextVar to store state per thread or async tasks.

Usage

Postpone marked requests handling until after the current handler has finished.

This is useful for dispatching requests from inside a transactional context. The middleware will pause proceessing  the middleware chain further and return. Proceessing the middleware chain will resume after the current request finishes.

The result of any postponed handlers will not be accessible.

Registration

Add the middleware to your bus.

bus = (
    banshee.builder()
    .with_middleware(banshee.HandleAfterMiddleware())
    .with_locator(registry)
    .build()
)

Context

You trigger this middleware with the HandleAfter context.

bus.handle(request, contexts=[banshee.HandleAfter()])

Reference

class banshee.HandleAfter

Bases: object

Handle after context.

Presence of this context marks that processing of the message should be postponed until after the current message has been handled.

class banshee.HandleAfterMiddleware

Bases: Middleware

Handle after middleware.

Postpone handling of specific messages until after the current handler has finished processing.

async __call__(message, handle)

Handle message.

Perform any processing on the message and forward it to the next handler in the chain.

Parameters:
Returns:

processed message

Raises:

banshee.errors.MultipleErrors – when the postponed handlers raise errors

Return type:

Message[T]