python-banshee icon indicating copy to clipboard operation
python-banshee copied to clipboard

Synchronous handlers

Open danielknell opened this issue 1 year ago • 0 comments

Description

Allow registering regular functions as handlers.

Motivation

Sometimes asynchronous options are not available for handling a request, to handle these cases it would be useful to be able to register regular functions as handlers instead of co-routines.

@registry.subscribe_to(CreateWidget)
def handler(command: CreateWidget, bus: banshee.SyncBus) -> None:
    # synchronous task here
    ...

    bus.handle(WidgetCreated(id=command.id))

Possible implementation

Update the Registry class to allow synchronous functions to be subscribed to requests, and wrap them in a thread pool before registering via loop.run_in_executor.

These synchronous handlers would also need a synchronous wrapper around the MessageBus class to then further publish events.

This will need additional work and documentation around the injector integration to minimise problems with sharing resources between threads.

danielknell avatar May 31 '24 10:05 danielknell