# Daemon Lifecycle Use {func}`jcmsuite.daemon.handler() ` to get the {class}`DaemonHandler ` for the active {class}`SolverContext `. Pass `context=` to select another context. The handler manages daemon lifecycle state and resource declarations. ## Open A Handler Use a {class}`DaemonHandler ` as a context manager when one block must manage the daemon connection: ```python import jcmsuite daemon = jcmsuite.daemon.handler() daemon.resources.add(jcmsuite.daemon.Workstation(hostname="localhost")) with daemon: result = jcmsuite.solve("project.jcmp") ``` When the `with` block begins, `jcmsuite` starts and connects the daemon. The handler is active for the block. When the block ends, `jcmsuite` disconnects the handler and stops the daemon. ```{note} When the application makes many solve calls, place the daemon context manager around all of them. Do not open and close a daemon context for each solve call because each context startup and shutdown adds lifecycle overhead. The same context can cover solve calls for projects in different files. ``` If startup or connection fails, the context manager raises an exception. Check the selected context, daemon installation, resource declarations, and remote authentication before you retry. Use explicit lifecycle calls when the {class}`DaemonHandler ` must remain available for several operations: ```python daemon = jcmsuite.daemon.handler() daemon.resources.add(jcmsuite.daemon.Workstation(hostname="localhost")) daemon.lifecycle.startup() result = jcmsuite.solve("project.jcmp") daemon.lifecycle.shutdown() ``` {meth}`startup() ` starts and connects the daemon. {meth}`shutdown() ` disconnects the handler and releases daemon service resources. You can connect the handler again after shutdown. ## Inspect Status Call {meth}`daemon.lifecycle.status() ` to inspect the status of the daemon: ```python status = daemon.lifecycle.status() print(status.started) print(status.connected) print(status.resource_count) ``` {attr}`started ` reports the daemon lifecycle state. {attr}`connected ` reports the handler connection state. The {class}`DaemonResources ` collection contains the configured resource declarations. The {attr}`daemon_payload ` contains backend status data when the daemon provides it. ## Select The Backend The backend mode in the context or solve call determines whether `jcmsuite` uses the daemon. The default backend mode is `"auto"`. Therefore, ```python with daemon: result = jcmsuite.solve("project.jcmp") ``` automatically uses the daemon. The same applies when the backend is `"daemon"`. When the backend is `"local"`, `jcmsuite` does not use the daemon, even in a `with daemon:` context. Use [Resources and Queues](resources.md) next to declare a local, scheduler, or remote resource. Use [Submit and Wait](submit_wait.md) when you need to collect a {term}`submission` later. --- Continue to [Resources and Queues](resources.md) for resource declarations and [Submit and Wait](submit_wait.md) for deferred collection. See the [Manage A Daemon Lifecycle tutorial](../../tutorials/execution_runtime/daemon/01_lifecycle.md) for a runnable local workflow.