patroni.postgresql.cancellable module

class patroni.postgresql.cancellable.CancellableExecutor

Bases: object

There must be only one such process so that AsyncExecutor can easily cancel it.

__init__() → None

Initialize self. See help(type(self)) for accurate signature.

_kill_children() → None
_kill_process() → None
_start_process(cmd: List[str], *args: Any, **kwargs: Any) → Optional[bool]

This method must be executed only when the _lock is acquired

class patroni.postgresql.cancellable.CancellableSubprocess

Bases: patroni.postgresql.cancellable.CancellableExecutor

__init__() → None

Initialize self. See help(type(self)) for accurate signature.

_communicate_streaming(stream_cb: Callable[[str, bytes], None]) → Tuple[bytes, bytes]

Read the running process’ stdout/stderr line-by-line as it arrives.

stderr is drained by a single helper thread while stdout is read inline, so both pipes are drained concurrently with only one extra thread. read1() returns as soon as any data is available, so slowly produced lines are delivered promptly. Lines are split on LF, CRLF, or a bare CR, so that progress reports of old pg_basebackup/pg_rewind versions are streamed as well. Exceptions from stream_cb are swallowed so streaming continues; only read/OS errors abort (after both streams are drained).

Parameters

stream_cb – called as stream_cb(stream_name, line) for every complete line of stdout/stderr.

Returns

(stdout_bytes, stderr_bytes).

call(*args: Any, **kwargs: Any) → Optional[int]

Start a cancellable subprocess, optionally capture/stream its output, and wait for it to finish.

Positional and keyword arguments are forwarded to psutil.Popen, except for the two special keyword arguments below, which are consumed here.

Parameters
  • communicate – optional dict used to exchange data with the process. When provided, stdout and stderr are captured and stored back into it under the stdout and stderr keys, and an optional input value is sent to stdin.

  • stream_cb – optional callback invoked as stream_cb(stream_name, line) for every complete line of stdout/stderr as it arrives. Effective only when there is no input to send; forces binary pipes.

Raises

PostgresException – if the executor was already cancelled.

Returns

the process’ exit code, or None if it could not be started.

cancel(kill: bool = False) → None
property is_cancelled
reset_is_cancelled() → None