Package: python-event-bus
InstallationBuilt and tested on Python 3.10 and above.No requirements other than the module itself.pipinstallpython-event-busExample UsageSubscribing to and calling an eventfrompython_event_busimportEventBus@EventBus.on("example_event")defon_example_event():print("Hello, World!")EventBus.call("example_event")OutputHello, World!Unsubscribing from an eventUnsubscribing from an event will no longer invoke the subscribed method when the event is called.frompython_event_busimportEventBus@EventBus.on("example_event")defon_example_event():print("Hello, World!")EventBus.unsubscribe("example_event",on_example_event)EventBus.call("example_event")# on_example_event will not be called because the subscription is no longer active.Calling an event with a single positional argumentfrompython_event_busimportEventBus@EventBus.on("example_event")defon_example_event(data):print(f"Received data:{data}")EventBus.call("example_event",True)OutputReceived data: TrueCalling an event with *argsfrompython_event_busimportEventBus@EventBus.on("example_event")defon_example_event(*args):print("Event called with the following arguments:")forargumentinargs:print(f" -{argument}")EventBus.call("example_event",1,2,3)OutputEvent called with the following arguments:
 - 1
 - 2
 - 3Calling an event with **kwargsfrompython_event_busimportEventBus@EventBus.on("example_event")defon_example_event(*args,**kwargs):print("Event called with the following arguments:")forargumentinargs:print(f" -{argument}")forkw_argumentinkwargs:print(f" -{kw_argument}={kwargs[kw_argument]}")EventBus.call("example_event",False,value=10,text="Hello")OutputEvent called with the following arguments:
 - False
 - value = 10
 - text = HelloSubscribing a method to an event without a decoratorfrompython_event_busimportEventBusdefon_example_event():print("Hello, World!")EventBus.subscribe("example_event",on_example_event)# on_example_event is subscribed to the event "example_event"EventBus.call("example_event")OutputHello, World!Using the event bus throughout different filesmain.pyfrompython_event_busimportEventBusfromtestimportcall_example_event@EventBus.on("example_event")defon_example_event(data):print(f"Example event called with data:{data}")call_example_event()test.pyfrompython_event_busimportEventBusdefcall_example_event():EventBus.call("example_event","Hello from test.py")OutputExample event called with data: Hello from test.pyEvent callbacks with different prioritiesA higher priority indicates a higher priority of callback. The default priority is 1.frompython_event_busimportEventBus@EventBus.on("example_event",priority=0)deflow_priority():print("Low priority")@EventBus.on("example_event",priority=5)defhigh_priority():print("High priority")@EventBus.on("example_event")defdefault_priority():print("Default priority")EventBus.call("example_event")OutputHigh priority
Default priority
Low priorityUsing the event bus context managerNote that existing methods such assubscribe,unsubscribe, andcallwill still function correctly inside of a context manager.frompython_event_busimportEventBus,EventBusContextManagerwithEventBusContextManager()asevent_bus:# Use the context manager to create methods# that are subscribed to an event only for# the lifetime of the context-managed bus.@event_bus.on("example_event")defon_example_event(data):print(f"Example event was called. Data:{data}")# Use the EventBus class to call events even if they are created within the context manager.EventBus.call("example_event",1)# All methods that are subscribed to events during the lifetime of the context# manager will be automatically unsubscribed when the context manager exits.# The subscription on_example_event that was created within the context manager# is no longer active.EventBus.call("example_event",2)# on_example_event will not be called.OutputExample event was called. Data: 1