Zope Event
The `zope.event` package provides a very basic, synchronous event publishing system. It enables application code to generate events without requiring awareness of the specific frameworks that might handle them. The current version is 6.1, released on 2025-11-07. Zope ecosystem packages typically follow semantic versioning, with minor releases approximately every 2-6 months and major releases every 2-3 years.
Warnings
- breaking Version 6.0 replaced `pkg_resources` namespace with PEP 420 native namespaces, which might require `zc.buildout` version 5 if your project uses it.
- breaking Version 6.1 dropped support for Python 3.9 and removed `setuptools` as a runtime dependency.
- breaking Version 5.0 dropped support for Python 2.7, 3.5, and 3.6. Version 5.1 further dropped support for Python 3.7 and 3.8.
- gotcha The `zope.event.notify()` function is synchronous. It calls all registered subscribers in sequential order, and the notification process will block until all subscribers have completed their execution.
- gotcha While direct manipulation of `zope.event.subscribers` (e.g., `zope.event.subscribers.append()`) is possible, it is generally discouraged for application-level event handling in favor of higher-level frameworks built on top of `zope.event`, such as `zope.component`.
Install
-
pip install zope.event
Imports
- notify
from zope.event import notify
- subscribers
from zope.event import subscribers
Quickstart
import zope.event
class MyEvent:
def __init__(self, data):
self.data = data
def my_subscriber(event):
print(f"Subscriber 1 received event with data: {event.data}")
def another_subscriber(event):
print(f"Subscriber 2 processed event: {event.data.upper()}")
# Clear existing subscribers for example isolation (optional for real apps)
# You might not want to do this in production, especially if other parts
# of your application register global subscribers.
_old_subscribers = zope.event.subscribers[:]
del zope.event.subscribers[:]
# Register subscribers
zope.event.subscribers.append(my_subscriber)
zope.event.subscribers.append(another_subscriber)
# Create and notify an event
event = MyEvent("hello world")
zope.event.notify(event)
# Unsubscribe (optional)
zope.event.subscribers.remove(my_subscriber)
zope.event.subscribers.remove(another_subscriber)
# Restore original subscribers (if applicable)
zope.event.subscribers.extend(_old_subscribers)
# Example of notifying with a simple object (any object can be an event)
zope.event.notify(42)
zope.event.notify("a string event")
print("\nDemonstrating direct manipulation of subscribers (for testing/setup):")
# Direct manipulation of the subscribers list is often used in tests
# or initialization, but higher-level frameworks like zope.component
# provide more sophisticated subscription mechanisms.
print(f"Current subscribers: {zope.event.subscribers}")