Skip to content

ezpubsub reference

Bases: Generic[SignalT]

A simple synchronous and asynchronous pub/sub signal.

error_raising property

Check if error raising is enabled.

frozen property

Check if the signal is frozen.

logging_enabled property

Check if logging is enabled.

require_freeze property

Check if freeze is required before publishing.

subscriber_count property

Return the total number of subscribers (both weak and strong).

__call__(func)

Decorator interface for subscribing functions.

Parameters:

Name Type Description Default
func Union[Callable[[SignalT], None], Callable[[SignalT], Awaitable[Any]]]

The function to subscribe.

required

Returns: The same function (for decorator chaining).

Usage example: ``` @signal def my_handler(data): print(f"Received data: {data}")

__len__()

Return self.subscriber_count using len for convenience.

aon_error(subscriber, callback, error) async

Async version of on_error. Override this to handle async errors differently.

Parameters:

Name Type Description Default
subscriber Any

The subscriber that raised the error.

required
callback Union[Callable[[SignalT], None], Callable[[SignalT], Awaitable[Any]]]

The callback that raised the error.

required
error Exception

The exception that was raised.

required

apublish(data, also_sync=False) async

Publish data to all async subscribers. Additionally use the also_sync flag to include all sync subscribers.

If any subscriber raises an exception, it will be caught and passed to the on_error method (which just logs by default, but can be overridden for custom error handling).

Parameters:

Name Type Description Default
data SignalT

The data to send to subscribers.

required
also_sync bool

If True, all sync subscribers will also be called in the current thread.

False

Raises: SignalError: If signal requires freeze and is not frozen. (Optional) Exception: If a subscriber's callback raises an exception, and error_raising is True, it will be raised after calling aon_error.

clear()

Clear all subscribers.

freeze()

Freeze the signal to prevent new subscriptions.

log(message, level=logging.INFO)

Override this to customize logging behavior. This will also override the logging_enabled flag.

Parameters:

Name Type Description Default
message str

The message to log.

required

on_error(subscriber, callback, error)

Override this to handle errors differently. This will also override the error_raising flag.

Parameters:

Name Type Description Default
subscriber Any

The subscriber that raised the error.

required
callback Union[Callable[[SignalT], None], Callable[[SignalT], Awaitable[Any]]]

The callback that raised the error.

required
error Exception

The exception that was raised.

required

publish(data)

Publish data to all synchronous subscribers (Async subscribers will be skipped).

If any subscriber raises an exception, it will be caught and passed to the on_error method (which just logs by default, but can be overridden for custom error handling).

Parameters:

Name Type Description Default
data SignalT

The data to send to subscribers.

required

Raises: (Optional) Exception: If a subscriber's callback raises an exception, and error_raising is True, it will be raised after calling on_error.

subscribe(callback)

Subscribe to the signal with a callback (sync or async).

Parameters:

Name Type Description Default
callback Union[Callable[[SignalT], None], Callable[[SignalT], Awaitable[Any]]]

A callable that accepts a single argument of type SignalT.

required

Raises: SignalError: If the callback is not callable or signal is frozen.

toggle_error_raising(enabled=True)

Toggle whether to raise exceptions in subscriber callbacks which are passed to on_error.

Note that you can also override the on_error method to customize error handling, which would also override this flag unless you chose to incorporate it.

toggle_logging(enabled=True)

Toggle logging for this signal.

Note that you can also override the log method to customize logging behavior, which would also override this flag unless you chose to incorporate it.

unsubscribe(subscriber)

Unsubscribe a subscriber from the signal.

Parameters:

Name Type Description Default
subscriber Any

The subscriber to remove, which can be a class instance or a function.

required

Returns: bool: True if the subscriber was removed, False if it was not found.