Zope Process Lifetime
zope.processlifetime provides an API for registering subscribers to events occurring during a process's lifecycle, such as starting, initialized, and stopping. It is a core component of the Zope Toolkit, enabling robust resource management and cleanup in Zope-based applications. The current version is 4.0, with releases typically following major Python version updates or Zope Foundation initiatives.
Common errors
-
ImportError: cannot import name 'registerProcessSubscriber' from 'zope.processlifetime'
cause Typo in the import statement or trying to import from an incorrect path (e.g., a submodule).fixVerify the import path is exactly `from zope.processlifetime import registerProcessSubscriber`. Ensure `zope.processlifetime` is correctly installed in your environment. -
TypeError: registerProcessSubscriber() missing 1 required positional argument: 'subscriber'
cause The `registerProcessSubscriber` function was called without passing the callable handler function as its first argument.fixProvide a callable function or method as the first argument to `registerProcessSubscriber`, for example: `registerProcessSubscriber(your_handler_function, IProcessStarting)`. -
AttributeError: module 'zope.event' has no attribute 'notify'
cause The `zope.event` library (which `zope.processlifetime` relies on for dispatch) is not installed or the `notify` function isn't being imported/used correctly for manual event triggering.fixEnsure `zope.event` is installed (`pip install zope.event`) and that you have `import zope.event` in your code before attempting to call `zope.event.notify()`.
Warnings
- breaking Version 4.0+ of `zope.processlifetime` requires Python 3.9 or higher. Older versions (e.g., 3.x) supported Python 2.7 and Python 3.5-3.8. Upgrading from older Python versions or older `zope.processlifetime` versions will require updating your Python environment.
- gotcha The events (e.g., `IProcessStarting`) are *not* automatically dispatched by `zope.processlifetime` itself. This library only provides the registration mechanism. Event dispatch is typically handled by a higher-level Zope-based framework or application server (like Zope or Plone) using `zope.event.notify()` at appropriate stages of its lifecycle.
- gotcha It's easy to confuse the process lifetime events with other event systems or directly calling handler functions. Ensure you are registering your handler with the correct `IProcess*` interface from `zope.processlifetime`.
Install
-
pip install zope.processlifetime
Imports
- registerProcessSubscriber
from zope.processlifetime import registerProcessSubscriber
- IProcessStarting
from zope.processlifetime.interfaces import IProcessStarting
from zope.processlifetime import IProcessStarting
- IProcessInitialized
from zope.processlifetime import IProcessInitialized
- IProcessStopping
from zope.processlifetime import IProcessStopping
Quickstart
import os
from zope.processlifetime import registerProcessSubscriber, IProcessStarting, IProcessInitialized, IProcessStopping
from zope.interface import implementer
import zope.event # Required for dispatching events
# Define handler functions that will react to events
def handle_startup(event):
print("Process starting event received!")
# Typically, you would initialize resources here
assert isinstance(event, IProcessStarting)
def handle_initialized(event):
print("Process initialized event received!")
# Typically, you would confirm readiness or start background tasks here
assert isinstance(event, IProcessInitialized)
def handle_stopping(event):
print("Process stopping event received!")
# Typically, you would clean up resources or save state here
assert isinstance(event, IProcessStopping)
# Register the handler functions for the respective lifecycle interfaces
registerProcessSubscriber(handle_startup, IProcessStarting)
registerProcessSubscriber(handle_initialized, IProcessInitialized)
registerProcessSubscriber(handle_stopping, IProcessStopping)
print("Subscribers registered. Now simulating event dispatch:")
# In a real Zope application, a framework or server would dispatch these events.
# For demonstration, we create and notify them explicitly using zope.event.
# Define simple event objects that implement the interfaces
@implementer(IProcessStarting)
class ProcessStartingEvent: pass
@implementer(IProcessInitialized)
class ProcessInitializedEvent: pass
@implementer(IProcessStopping)
class ProcessStoppingEvent: pass
# Dispatch the events to trigger the registered handlers
zope.event.notify(ProcessStartingEvent())
zope.event.notify(ProcessInitializedEvent())
zope.event.notify(ProcessStoppingEvent())
print("\nEvents dispatched. Check console output for handler messages.")
# Note: os.environ.get('AUTH_KEY', '') is not relevant for this library's quickstart.