Go to the latest version of this document.

# ``twisted`.`runner`.`procmon`.`ProcessMonitor``

class documentation

`class ProcessMonitor(service.Service): (source)`

Constructor: `ProcessMonitor(reactor)`

View In Hierarchy

ProcessMonitor runs processes, monitors their progress, and restarts them when they die.

The ProcessMonitor will not attempt to restart a process that appears to die instantly -- with each "instant" death (less than 1 second, by default), it will delay approximately twice as long before restarting it. A successful run will reset the counter.

The primary interface is `addProcess` and `removeProcess`. When the service is running (that is, when the application it is attached to is running), adding a process automatically starts it.

Each process has a name. This name string must uniquely identify the process. In particular, attempting to add two processes with the same name will result in a `KeyError`.

Method | `__getstate__` | Undocumented
Method | `__init__` | Undocumented
Method | `__repr__` | Undocumented
Method | `addProcess` | Add a new monitored process and start it immediately if the `ProcessMonitor` service is running.
Method | `connectionLost` | Called when a monitored processes exits. If `service.IService.running` is `True` (ie the service is started), the process will be restarted. If the process had been running for more than `ProcessMonitor.threshold`...
Method | `removeProcess` | Stop the named process and remove it from the list of monitored processes.
Method | `restartAll` | Restart all processes. This is useful for third party management services to allow a user to restart servers because of an outside change in circumstances -- for example, a new version of a library is installed.
Method | `startProcess` | No summary
Method | `startService` | Start all monitored processes.
Method | `stopProcess` | No summary
Method | `stopService` | Stop all monitored processes and cancel all scheduled process restarts.
Instance Variable | `delay` | Undocumented
Instance Variable | `killTime` | How long a process being killed has to get its affairs in order before it gets killed with an unmaskable signal. The default value is 5 seconds.
Instance Variable | `log` | The logger used to propagate log messages from spawned processes.
Instance Variable | `maxRestartDelay` | The maximum time (in seconds) to wait before attempting to restart a process. Default 3600s (1h).
Instance Variable | `minRestartDelay` | The minimum time (in seconds) to wait before attempting to restart a process. Default 1s.
Instance Variable | `murder` | Undocumented
Instance Variable | `protocols` | Undocumented
Instance Variable | `restart` | Undocumented
Instance Variable | `threshold` | How long a process has to live before the death is considered instant, in seconds. The default value is 1 second.
Instance Variable | `timeStarted` | Undocumented
Property | `processes` | Processes as dict of tuples
Method | `_forceStopProcess` | Undocumented
Method | `_monitoredProcessExited` | Called when a monitored processes exits. If `service.IService.running` is `True` (ie the service is started), the process will be restarted. If the process had been running for more than `ProcessMonitor.threshold`...
Instance Variable | `_clock` | Undocumented
Instance Variable | `_processes` | Undocumented
Instance Variable | `_reactor` | A provider of `IReactorProcess` and `IReactorTime` which will be used to spawn processes and register delayed calls.

Inherited from `Service`:

Method | `disownServiceParent` | Use this API to remove an `IService` from an `IServiceCollection`.
Method | `privilegedStartService` | Do preparation work for starting the service.
Method | `setName` | Set the name of the service.
Method | `setServiceParent` | Set the parent of the service. This method is responsible for setting the `parent` attribute on this service (the child service).
Instance Variable | `name` | A `str` which is the name of the service or `None`.
Instance Variable | `parent` | An `IServiceCollection` which is the parent or `None`.
Instance Variable | `running` | A `boolean` which indicates whether the service is running.

def __getstate__(self) -> `Any`: (source)
overrides `twisted.application.service.Service.__getstate__`

Deprecated since version 18.7.0: `__getstate__` was deprecated in Twisted 18.7.0.

Undocumented

def __init__(self, reactor: `IReactorProcess` = _reactor): (source)

Undocumented

def __repr__(self) -> `str`: (source)

Undocumented

def addProcess(self, name: `str`, args: `list[str]`, uid: `int | None` = None, gid: `int | None` = None, env: `dict[str, str]` = {}, cwd: `str | None` = None): (source)

Add a new monitored process and start it immediately if the `ProcessMonitor` service is running.

Note that args are passed to the system call, not to the shell. If running the shell is desired, the common idiom is to use `ProcessMonitor.addProcess("name", ['/bin/sh', '-c', shell_script])`

Parameters
name:`str` | A name for this process. This value must be unique across all processes added to this monitor.
args:`list[str]` | The argv sequence for the process to launch.
uid:`int | None` | The user ID to use to run the process. If `None`, the current UID is used.
gid:`int | None` | The group ID to use to run the process. If `None`, the current GID is used.
env:`dict[str, str]` | The environment to give to the launched process. See `IReactorProcess.spawnProcess`'s `env` parameter.
cwd:`str | None` | The initial working directory of the launched process. The default of `None` means inheriting the laucnhing process's working directory.

Raises
`KeyError` | If a process with the given name already exists.

def connectionLost(self, name: `str`): (source)

Deprecated since version 25.5.0: `connectionLost` was deprecated in Twisted 25.5.0.

Called when a monitored processes exits. If `service.IService.running` is `True` (ie the service is started), the process will be restarted. If the process had been running for more than `ProcessMonitor.threshold` seconds it will be restarted immediately. If the process had been running for less than `ProcessMonitor.threshold` seconds, the restart will be delayed and each time the process dies before the configured threshold, the restart delay will be doubled - up to a maximum delay of maxRestartDelay sec.

Parameters
name:`str` | A string that uniquely identifies the process which exited.

def removeProcess(self, name: `str`): (source)

Stop the named process and remove it from the list of monitored processes.

Parameters
name:`str` | A string that uniquely identifies the process.

def restartAll(self): (source)

Restart all processes. This is useful for third party management services to allow a user to restart servers because of an outside change in circumstances -- for example, a new version of a library is installed.

def startProcess(self, name: `str`): (source)

Parameters
name:`str` | The name of the process to be started

def startService(self): (source)
from `twisted.application.service.IService`
overrides `twisted.application.service.Service.startService`

Start all monitored processes.

def stopProcess(self, name: `str`): (source)

Parameters
name:`str` | The name of the process to be stopped

def stopService(self): (source)
from `twisted.application.service.IService`
overrides `twisted.application.service.Service.stopService`

Stop all monitored processes and cancel all scheduled process restarts.

delay: `dict[str, float]` = (source)

Undocumented

killTime: `float` = (source)

How long a process being killed has to get its affairs in order before it gets killed with an unmaskable signal. The default value is 5 seconds.

log: `Logger` = (source)

The logger used to propagate log messages from spawned processes.

maxRestartDelay: `float` = (source)

The maximum time (in seconds) to wait before attempting to restart a process. Default 3600s (1h).

minRestartDelay: `float` = (source)

The minimum time (in seconds) to wait before attempting to restart a process. Default 1s.

murder: `dict[str, IDelayedCall]` = (source)

Undocumented

protocols: `dict[str, LoggingProtocol]` = (source)

Undocumented

restart: `dict[str, IDelayedCall]` = (source)

Undocumented

threshold: `float` = (source)

How long a process has to live before the death is considered instant, in seconds. The default value is 1 second.

timeStarted: `dict[str, float]` = (source)

Undocumented

processes: `dict[str, tuple[list[str], int | None, int | None, dict[str, str]]]` = (source)

Deprecated since version 18.7.0: `processes` was deprecated in Twisted 18.7.0.

Processes as dict of tuples

def _forceStopProcess(self, proc: `IProcessTransport`): (source)

Undocumented

def _monitoredProcessExited(self, name: `str`, reason: `Failure | None` = None): (source)

Called when a monitored processes exits. If `service.IService.running` is `True` (ie the service is started), the process will be restarted. If the process had been running for more than `ProcessMonitor.threshold` seconds it will be restarted immediately. If the process had been running for less than `ProcessMonitor.threshold` seconds, the restart will be delayed and each time the process dies before the configured threshold, the restart delay will be doubled - up to a maximum delay of maxRestartDelay sec.

Parameters
name:`str` | A string that uniquely identifies the process which exited.
reason:`Failure | None` | The reason why the connection was lost.

_clock = (source)

Undocumented

_processes: `dict[str, _Process]` = (source)

Undocumented

_reactor: `IReactorProcess` provider = (source)

A provider of `IReactorProcess` and `IReactorTime` which will be used to spawn processes and register delayed calls.