Skip to content

multieventhandler

Class info

Classes

Name Children Inherits
MultiEventHandler
anyenv.calling.multieventhandler
Manages multiple callbacks/event handlers with various execution modes.

    🛈 DocStrings

    Generic async callback manager for handling multiple event handlers.

    MultiEventHandler

    Manages multiple callbacks/event handlers with various execution modes.

    Provides a unified interface for executing multiple callbacks either sequentially, in parallel, or as background tasks, with support for dynamic handler management and optional debouncing. Sync functions are automatically wrapped to work with the async interface.

    The generic parameters HandlerT and Mode provide type safety: - HandlerT: callable type (function signature) - Mode: execution mode literal for return type inference

    Parameters:

    Name Type Description Default
    handlers Sequence[HandlerT] | HandlerT | None

    Initial list of async or sync callable event handlers, or single handler

    None
    mode Mode

    Execution mode - "sequential", "parallel", or "task"

    'parallel'
    debounce_delay float | None

    Optional delay in seconds for debouncing rapid calls

    None
    Example
    from collections.abc import Awaitable
    from typing import Callable, Literal
    
    async def async_handler(x: int, y: str) -> str:
        return f"Async Handler: {x}, {y}"
    
    def sync_handler(x: int, y: str) -> str:
        return f"Sync Handler: {x}, {y}"
    
    # Type-safe handler with task mode
    Handler = Callable[[int, str], str | Awaitable[str]]
    manager = MultiEventHandler[Handler, Literal["task"]](
        [async_handler, sync_handler],
        "task",
        debounce_delay=0.1
    )
    tasks: list[asyncio.Task[Any]] = await manager(42, "test")
    
    Source code in src/anyenv/calling/multieventhandler.py
    class MultiEventHandler[HandlerT: Callable[..., Any], Mode: ExecutionMode = DefaultMode]:
        """Manages multiple callbacks/event handlers with various execution modes.
    
        Provides a unified interface for executing multiple callbacks either sequentially,
        in parallel, or as background tasks, with support for dynamic handler management
        and optional debouncing. Sync functions are automatically wrapped to work with
        the async interface.
    
        The generic parameters HandlerT and Mode provide type safety:
        - HandlerT: callable type (function signature)
        - Mode: execution mode literal for return type inference
    
        Args:
            handlers: Initial list of async or sync callable event handlers, or single handler
            mode: Execution mode - "sequential", "parallel", or "task"
            debounce_delay: Optional delay in seconds for debouncing rapid calls
    
        Example:
            ```python
            from collections.abc import Awaitable
            from typing import Callable, Literal
    
            async def async_handler(x: int, y: str) -> str:
                return f"Async Handler: {x}, {y}"
    
            def sync_handler(x: int, y: str) -> str:
                return f"Sync Handler: {x}, {y}"
    
            # Type-safe handler with task mode
            Handler = Callable[[int, str], str | Awaitable[str]]
            manager = MultiEventHandler[Handler, Literal["task"]](
                [async_handler, sync_handler],
                "task",
                debounce_delay=0.1
            )
            tasks: list[asyncio.Task[Any]] = await manager(42, "test")
            ```
        """
    
        def __init__(
            self,
            handlers: Sequence[HandlerT] | HandlerT | None = None,
            # Only valid while Mode is the default; type checkers cannot express that.
            mode: Mode = "parallel",  # ty: ignore[invalid-parameter-default]
            debounce_delay: float | None = None,
        ) -> None:
            self._handlers: list[HandlerT] = []
            self._wrapped_handlers: list[Callable[..., Awaitable[Any]]] = []
            self._handler_mapping: dict[HandlerT, Callable[..., Awaitable[Any]]] = {}
            self._mode: Mode = mode
            self._debounce_delay = debounce_delay
            # The round still in its delay (a new call replaces it), the callers waiting
            # for that round, and every round task (asyncio keeps only weak references).
            self._debounce_task: asyncio.Task[Any] | None = None
            self._debounce_waiters: list[asyncio.Future[Any]] = []
            self._debounce_rounds: set[asyncio.Task[Any]] = set()
    
            if handlers is not None:
                match handlers:
                    case Sequence():
                        for handler in handlers:
                            self.add_handler(handler)
                    case _:
                        # Single handler
                        self.add_handler(handlers)
    
        async def __call__(self, *args: Any, **kwargs: Any) -> Any:
            """Execute all handlers with the configured mode and debouncing.
    
            Returns:
                - For "sequential"/"parallel": list[Any] (handler results)
                - For "task": list[asyncio.Task[Any]] (task objects)
            """
            if self._debounce_delay is not None:
                return await self._execute_debounced(self._mode, *args, **kwargs)
            return await self._execute_handlers(self._mode, *args, **kwargs)
    
        @overload
        async def call_handlers(
            self, mode: Literal["task"], *args: Any, **kwargs: Any
        ) -> list[asyncio.Task[Any]]: ...
    
        @overload
        async def call_handlers(
            self, mode: Literal["sequential", "parallel"], *args: Any, **kwargs: Any
        ) -> list[Any]: ...
    
        async def call_handlers(
            self, mode: ExecutionMode, *args: Any, **kwargs: Any
        ) -> list[Any] | list[asyncio.Task[Any]]:
            """Execute handlers with explicit mode, bypassing configured mode and debouncing.
    
            Args:
                mode: Execution mode to use for this call
                *args: Arguments to pass to handlers
                **kwargs: Keyword arguments to pass to handlers
    
            Returns:
                - For "sequential"/"parallel": list[Any] (handler results)
                - For "task": list[asyncio.Task[Any]] (task objects)
            """
            return await self._execute_handlers(mode, *args, **kwargs)
    
        async def _execute_debounced(
            self, mode: ExecutionMode, *args: Any, **kwargs: Any
        ) -> list[Any] | list[asyncio.Task[Any]]:
            """Execute handlers with debouncing.
    
            A call within the delay replaces the pending one. Callers of replaced
            calls get the result of the call that runs (they are not cancelled).
            """
            if self._debounce_task is not None and not self._debounce_task.done():
                self._debounce_task.cancel()
            waiter: asyncio.Future[Any] = asyncio.get_running_loop().create_future()
            self._debounce_waiters.append(waiter)
            task = asyncio.create_task(self._debounce_round(mode, args, kwargs))
            self._debounce_task = task
            self._debounce_rounds.add(task)
            task.add_done_callback(self._debounce_rounds.discard)
            return await waiter  # type: ignore[no-any-return]
    
        async def _debounce_round(
            self, mode: ExecutionMode, args: tuple[Any, ...], kwargs: dict[str, Any]
        ) -> None:
            if self._debounce_delay is not None:
                await anyio.sleep(self._debounce_delay)
            # Past the delay: this round runs for the callers so far; later calls start
            # a new round instead of cancelling this one.
            waiters, self._debounce_waiters = self._debounce_waiters, []
            if self._debounce_task is asyncio.current_task():
                self._debounce_task = None
            try:
                result = await self._execute_handlers(mode, *args, **kwargs)
            except BaseException as exc:
                for waiter in waiters:
                    if waiter.done():
                        continue
                    if isinstance(exc, asyncio.CancelledError):
                        waiter.cancel()
                    else:
                        waiter.set_exception(exc)
                if not isinstance(exc, Exception):
                    raise
                return  # delivered to the callers; the round task itself ends cleanly
            for waiter in waiters:
                if not waiter.done():
                    waiter.set_result(result)
    
        async def _execute_handlers(
            self, mode: ExecutionMode, *args: Any, **kwargs: Any
        ) -> list[Any] | list[asyncio.Task[Any]]:
            """Execute handlers with specified mode."""
            if not self._wrapped_handlers:
                return []
    
            match mode:
                case "sequential":
                    return await self._execute_sequential(*args, **kwargs)
                case "parallel":
                    return await self._execute_parallel(*args, **kwargs)
                case "task":
                    return self._execute_as_tasks(*args, **kwargs)
    
        async def _execute_sequential(self, *args: Any, **kwargs: Any) -> list[Any]:
            """Execute handlers sequentially."""
            return [await handler(*args, **kwargs) for handler in self._wrapped_handlers]
    
        async def _execute_parallel(self, *args: Any, **kwargs: Any) -> list[Any]:
            """Execute handlers in parallel using asyncio.gather."""
            tasks = [handler(*args, **kwargs) for handler in self._wrapped_handlers]
            return await asyncio.gather(*tasks)
    
        def _execute_as_tasks(self, *args: Any, **kwargs: Any) -> list[asyncio.Task[Any]]:
            """Execute handlers as background tasks (non-blocking)."""
            tasks: list[asyncio.Task[Any]] = []
            for handler in self._wrapped_handlers:
                result = handler(*args, **kwargs)
                # Ensure we have a coroutine for asyncio.create_task
                if inspect.iscoroutine(result):
                    task = asyncio.create_task(result)
                else:
                    # If it's not a coroutine, wrap it in one
                    async def wrapper(captured_result: Any = result) -> Any:
                        return captured_result
    
                    task = asyncio.create_task(wrapper())
                tasks.append(task)
            return tasks
    
        def add_handler(self, handler: HandlerT) -> None:
            """Add a new handler to the manager.
    
            Both sync and async handlers are supported.
            """
            if handler in self._handlers:
                return
    
            # Check if handler is already async (function or callable class)
            if inspect.iscoroutinefunction(handler):
                wrapped_handler = handler
            elif callable(handler) and inspect.iscoroutinefunction(handler.__call__):
                wrapped_handler = handler.__call__
            else:
                # Wrap sync handler
                wrapped_handler = self._wrap_sync_handler(handler)  # type: ignore[assignment]
    
            self._handlers.append(handler)
            self._wrapped_handlers.append(wrapped_handler)
            self._handler_mapping[handler] = wrapped_handler
    
        def remove_handler(self, handler: HandlerT) -> None:
            """Remove a handler from the manager.
    
            Note: For sync handlers, you must pass the original sync function,
            not the wrapped async version.
            """
            if handler not in self._handlers:
                return
    
            with contextlib.suppress(ValueError):
                # Remove from all tracking structures
                wrapped_handler = self._handler_mapping[handler]
                self._handlers.remove(handler)
                self._wrapped_handlers.remove(wrapped_handler)
                del self._handler_mapping[handler]
    
        def _wrap_sync_handler(self, handler: HandlerT) -> Callable[..., Awaitable[Any]]:
            """Wrap a synchronous handler to work with async interface."""
    
            async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
                return handler(*args, **kwargs)
    
            return async_wrapper
    
        def clear(self) -> None:
            """Remove all handlers."""
            self._handlers.clear()
            self._wrapped_handlers.clear()
            self._handler_mapping.clear()
    
        @property
        def mode(self) -> Mode:
            """Current execution mode."""
            return self._mode
    
        @property
        def debounce_delay(self) -> float | None:
            """Current debounce delay in seconds."""
            return self._debounce_delay
    
        @debounce_delay.setter
        def debounce_delay(self, value: float | None) -> None:
            """Set debounce delay in seconds."""
            self._debounce_delay = value
    
        def __len__(self) -> int:
            """Return number of handlers."""
            return len(self._handlers)
    
        def __bool__(self) -> bool:
            """Return True if there are handlers registered."""
            return bool(self._handlers)
    
        def __repr__(self) -> str:
            """Return string representation showing handlers, mode, and debounce."""
            handler_names = [
                h.__qualname__ if hasattr(h, "__qualname__") else repr(h) for h in self._handlers
            ]
            debounce_info = f", debounce={self._debounce_delay}s" if self._debounce_delay else ""
            return f"MultiEventHandler(handlers={handler_names}, mode={self._mode!r}{debounce_info})"
    

    debounce_delay property writable

    debounce_delay: float | None
    

    Current debounce delay in seconds.

    mode property

    mode: Mode
    

    Current execution mode.

    __bool__

    __bool__() -> bool
    

    Return True if there are handlers registered.

    Source code in src/anyenv/calling/multieventhandler.py
    def __bool__(self) -> bool:
        """Return True if there are handlers registered."""
        return bool(self._handlers)
    

    __call__ async

    __call__(*args: Any, **kwargs: Any) -> Any
    

    Execute all handlers with the configured mode and debouncing.

    Returns:

    Type Description
    Any
    • For "sequential"/"parallel": list[Any] (handler results)
    Any
    • For "task": list[asyncio.Task[Any]] (task objects)
    Source code in src/anyenv/calling/multieventhandler.py
    async def __call__(self, *args: Any, **kwargs: Any) -> Any:
        """Execute all handlers with the configured mode and debouncing.
    
        Returns:
            - For "sequential"/"parallel": list[Any] (handler results)
            - For "task": list[asyncio.Task[Any]] (task objects)
        """
        if self._debounce_delay is not None:
            return await self._execute_debounced(self._mode, *args, **kwargs)
        return await self._execute_handlers(self._mode, *args, **kwargs)
    

    __len__

    __len__() -> int
    

    Return number of handlers.

    Source code in src/anyenv/calling/multieventhandler.py
    def __len__(self) -> int:
        """Return number of handlers."""
        return len(self._handlers)
    

    __repr__

    __repr__() -> str
    

    Return string representation showing handlers, mode, and debounce.

    Source code in src/anyenv/calling/multieventhandler.py
    def __repr__(self) -> str:
        """Return string representation showing handlers, mode, and debounce."""
        handler_names = [
            h.__qualname__ if hasattr(h, "__qualname__") else repr(h) for h in self._handlers
        ]
        debounce_info = f", debounce={self._debounce_delay}s" if self._debounce_delay else ""
        return f"MultiEventHandler(handlers={handler_names}, mode={self._mode!r}{debounce_info})"
    

    add_handler

    add_handler(handler: HandlerT) -> None
    

    Add a new handler to the manager.

    Both sync and async handlers are supported.

    Source code in src/anyenv/calling/multieventhandler.py
    def add_handler(self, handler: HandlerT) -> None:
        """Add a new handler to the manager.
    
        Both sync and async handlers are supported.
        """
        if handler in self._handlers:
            return
    
        # Check if handler is already async (function or callable class)
        if inspect.iscoroutinefunction(handler):
            wrapped_handler = handler
        elif callable(handler) and inspect.iscoroutinefunction(handler.__call__):
            wrapped_handler = handler.__call__
        else:
            # Wrap sync handler
            wrapped_handler = self._wrap_sync_handler(handler)  # type: ignore[assignment]
    
        self._handlers.append(handler)
        self._wrapped_handlers.append(wrapped_handler)
        self._handler_mapping[handler] = wrapped_handler
    

    call_handlers async

    call_handlers(mode: Literal['task'], *args: Any, **kwargs: Any) -> list[Task[Any]]
    
    call_handlers(mode: Literal['sequential', 'parallel'], *args: Any, **kwargs: Any) -> list[Any]
    
    call_handlers(mode: ExecutionMode, *args: Any, **kwargs: Any) -> list[Any] | list[Task[Any]]
    

    Execute handlers with explicit mode, bypassing configured mode and debouncing.

    Parameters:

    Name Type Description Default
    mode ExecutionMode

    Execution mode to use for this call

    required
    *args Any

    Arguments to pass to handlers

    ()
    **kwargs Any

    Keyword arguments to pass to handlers

    {}

    Returns:

    Type Description
    list[Any] | list[Task[Any]]
    • For "sequential"/"parallel": list[Any] (handler results)
    list[Any] | list[Task[Any]]
    • For "task": list[asyncio.Task[Any]] (task objects)
    Source code in src/anyenv/calling/multieventhandler.py
    async def call_handlers(
        self, mode: ExecutionMode, *args: Any, **kwargs: Any
    ) -> list[Any] | list[asyncio.Task[Any]]:
        """Execute handlers with explicit mode, bypassing configured mode and debouncing.
    
        Args:
            mode: Execution mode to use for this call
            *args: Arguments to pass to handlers
            **kwargs: Keyword arguments to pass to handlers
    
        Returns:
            - For "sequential"/"parallel": list[Any] (handler results)
            - For "task": list[asyncio.Task[Any]] (task objects)
        """
        return await self._execute_handlers(mode, *args, **kwargs)
    

    clear

    clear() -> None
    

    Remove all handlers.

    Source code in src/anyenv/calling/multieventhandler.py
    def clear(self) -> None:
        """Remove all handlers."""
        self._handlers.clear()
        self._wrapped_handlers.clear()
        self._handler_mapping.clear()
    

    remove_handler

    remove_handler(handler: HandlerT) -> None
    

    Remove a handler from the manager.

    Note: For sync handlers, you must pass the original sync function, not the wrapped async version.

    Source code in src/anyenv/calling/multieventhandler.py
    def remove_handler(self, handler: HandlerT) -> None:
        """Remove a handler from the manager.
    
        Note: For sync handlers, you must pass the original sync function,
        not the wrapped async version.
        """
        if handler not in self._handlers:
            return
    
        with contextlib.suppress(ValueError):
            # Remove from all tracking structures
            wrapped_handler = self._handler_mapping[handler]
            self._handlers.remove(handler)
            self._wrapped_handlers.remove(wrapped_handler)
            del self._handler_mapping[handler]
    

    SomeClass

    Some class.

    Source code in src/anyenv/calling/multieventhandler.py
    class SomeClass:
        """Some class."""
    
        def __call__(self, a: int, b: str) -> None:
            """Test class call method."""
            print(f"Class Handler: {a}, {b}")
    

    __call__

    __call__(a: int, b: str) -> None
    

    Test class call method.

    Source code in src/anyenv/calling/multieventhandler.py
    def __call__(self, a: int, b: str) -> None:
        """Test class call method."""
        print(f"Class Handler: {a}, {b}")
    

    async_handler async

    async_handler(a: int, b: str) -> None
    

    Async handler function.

    Source code in src/anyenv/calling/multieventhandler.py
    async def async_handler(a: int, b: str) -> None:
        """Async handler function."""
        await anyio.sleep(0.1)
        print(f"Async Handler: {a}, {b}")
    

    demo async

    demo() -> None
    

    Demo the handlers.

    Source code in src/anyenv/calling/multieventhandler.py
    async def demo() -> None:
        """Demo the handlers."""
        # Type-safe task execution
        tasks = await task_handler(42, "test")
        print(f"Created {len(tasks)} tasks")
    
        # Debounced parallel execution
        results = await parallel_handler(1, "hello")
        print(f"Parallel results: {results}")
    
        # Runtime mode switching
        task_results = await parallel_handler.call_handlers("task", 2, "world")
        print(f"Task mode results: {len(task_results)} tasks")
    

    handler

    handler(a: int, b: str) -> None
    

    Handler function.

    Source code in src/anyenv/calling/multieventhandler.py
    def handler(a: int, b: str) -> None:
        """Handler function."""
        print(f"Handler: {a}, {b}")