{"id":10370,"library":"zope-processlifetime","title":"Zope Process Lifetime","description":"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.","status":"active","version":"4.0","language":"en","source_language":"en","source_url":"https://github.com/zopefoundation/zope.processlifetime","tags":["zope","lifecycle","events","process","toolkit","framework"],"install":[{"cmd":"pip install zope.processlifetime","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides the event interfaces and mechanisms for event registration and dispatch required by zope.processlifetime.","package":"zope.interface","optional":false}],"imports":[{"symbol":"registerProcessSubscriber","correct":"from zope.processlifetime import registerProcessSubscriber"},{"note":"All core interfaces are directly exposed in the top-level package for convenience.","wrong":"from zope.processlifetime.interfaces import IProcessStarting","symbol":"IProcessStarting","correct":"from zope.processlifetime import IProcessStarting"},{"symbol":"IProcessInitialized","correct":"from zope.processlifetime import IProcessInitialized"},{"symbol":"IProcessStopping","correct":"from zope.processlifetime import IProcessStopping"}],"quickstart":{"code":"import os\nfrom zope.processlifetime import registerProcessSubscriber, IProcessStarting, IProcessInitialized, IProcessStopping\nfrom zope.interface import implementer\nimport zope.event # Required for dispatching events\n\n# Define handler functions that will react to events\ndef handle_startup(event):\n    print(\"Process starting event received!\")\n    # Typically, you would initialize resources here\n    assert isinstance(event, IProcessStarting)\n\ndef handle_initialized(event):\n    print(\"Process initialized event received!\")\n    # Typically, you would confirm readiness or start background tasks here\n    assert isinstance(event, IProcessInitialized)\n\ndef handle_stopping(event):\n    print(\"Process stopping event received!\")\n    # Typically, you would clean up resources or save state here\n    assert isinstance(event, IProcessStopping)\n\n# Register the handler functions for the respective lifecycle interfaces\nregisterProcessSubscriber(handle_startup, IProcessStarting)\nregisterProcessSubscriber(handle_initialized, IProcessInitialized)\nregisterProcessSubscriber(handle_stopping, IProcessStopping)\n\nprint(\"Subscribers registered. Now simulating event dispatch:\")\n\n# In a real Zope application, a framework or server would dispatch these events.\n# For demonstration, we create and notify them explicitly using zope.event.\n\n# Define simple event objects that implement the interfaces\n@implementer(IProcessStarting)\nclass ProcessStartingEvent: pass\n\n@implementer(IProcessInitialized)\nclass ProcessInitializedEvent: pass\n\n@implementer(IProcessStopping)\nclass ProcessStoppingEvent: pass\n\n# Dispatch the events to trigger the registered handlers\nzope.event.notify(ProcessStartingEvent())\nzope.event.notify(ProcessInitializedEvent())\nzope.event.notify(ProcessStoppingEvent())\n\nprint(\"\\nEvents dispatched. Check console output for handler messages.\")\n# Note: os.environ.get('AUTH_KEY', '') is not relevant for this library's quickstart.","lang":"python","description":"This quickstart demonstrates how to define handler functions and register them with `zope.processlifetime.registerProcessSubscriber` for specific process lifecycle events. It also shows how to explicitly dispatch these events using `zope.event.notify` for testing purposes, which is typically handled automatically by a Zope application server in a production environment."},"warnings":[{"fix":"Ensure your project's Python interpreter is version 3.9 or newer. Upgrade your Python environment if necessary.","message":"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.","severity":"breaking","affected_versions":"4.0+"},{"fix":"When testing or developing outside a full Zope environment, you must explicitly create event objects (implementing the respective `IProcess*` interfaces) and dispatch them using `zope.event.notify(your_event_object)` to trigger your subscribers.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Always import the specific event interfaces (e.g., `IProcessStarting`) from `zope.processlifetime` and pass them as the second argument to `registerProcessSubscriber`. Your handler function will receive an object that implements that interface.","message":"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`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Verify the import path is exactly `from zope.processlifetime import registerProcessSubscriber`. Ensure `zope.processlifetime` is correctly installed in your environment.","cause":"Typo in the import statement or trying to import from an incorrect path (e.g., a submodule).","error":"ImportError: cannot import name 'registerProcessSubscriber' from 'zope.processlifetime'"},{"fix":"Provide a callable function or method as the first argument to `registerProcessSubscriber`, for example: `registerProcessSubscriber(your_handler_function, IProcessStarting)`.","cause":"The `registerProcessSubscriber` function was called without passing the callable handler function as its first argument.","error":"TypeError: registerProcessSubscriber() missing 1 required positional argument: 'subscriber'"},{"fix":"Ensure `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()`.","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.","error":"AttributeError: module 'zope.event' has no attribute 'notify'"}]}