|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from collections.abc import Sequence |
| 5 | +from typing import Any, Protocol |
| 6 | + |
| 7 | +from plumpy import loaders |
| 8 | +from plumpy.message import MessageType |
| 9 | +from plumpy.utils import PID_TYPE |
| 10 | + |
| 11 | +ProcessResult = Any |
| 12 | +ProcessStatus = Any |
| 13 | + |
| 14 | + |
| 15 | +class ProcessController(Protocol): |
| 16 | + """ |
| 17 | + Control processes using coroutines that will send messages and wait |
| 18 | + (in a non-blocking way) for their response |
| 19 | + """ |
| 20 | + |
| 21 | + def get_status(self, pid: 'PID_TYPE') -> ProcessStatus: |
| 22 | + """ |
| 23 | + Get the status of a process with the given PID |
| 24 | + :param pid: the process id |
| 25 | + :return: the status response from the process |
| 26 | + """ |
| 27 | + ... |
| 28 | + |
| 29 | + def pause_process(self, pid: 'PID_TYPE', msg: Any | None = None) -> ProcessResult: |
| 30 | + """ |
| 31 | + Pause the process |
| 32 | +
|
| 33 | + :param pid: the pid of the process to pause |
| 34 | + :param msg: optional pause message |
| 35 | + :return: True if paused, False otherwise |
| 36 | + """ |
| 37 | + ... |
| 38 | + |
| 39 | + def play_process(self, pid: 'PID_TYPE') -> ProcessResult: |
| 40 | + """ |
| 41 | + Play the process |
| 42 | +
|
| 43 | + :param pid: the pid of the process to play |
| 44 | + :return: True if played, False otherwise |
| 45 | + """ |
| 46 | + ... |
| 47 | + |
| 48 | + def kill_process(self, pid: 'PID_TYPE', msg: MessageType | None = None) -> ProcessResult: |
| 49 | + """ |
| 50 | + Kill the process |
| 51 | +
|
| 52 | + :param pid: the pid of the process to kill |
| 53 | + :param msg: optional kill message |
| 54 | + :return: True if killed, False otherwise |
| 55 | + """ |
| 56 | + ... |
| 57 | + |
| 58 | + def continue_process( |
| 59 | + self, pid: 'PID_TYPE', tag: str | None = None, nowait: bool = False, no_reply: bool = False |
| 60 | + ) -> ProcessResult | None: |
| 61 | + """ |
| 62 | + Continue the process |
| 63 | +
|
| 64 | + :param _communicator: the communicator |
| 65 | + :param pid: the pid of the process to continue |
| 66 | + :param tag: the checkpoint tag to continue from |
| 67 | + """ |
| 68 | + ... |
| 69 | + |
| 70 | + async def launch_process( |
| 71 | + self, |
| 72 | + process_class: str, |
| 73 | + init_args: Sequence[Any] | None = None, |
| 74 | + init_kwargs: dict[str, Any] | None = None, |
| 75 | + persist: bool = False, |
| 76 | + loader: loaders.ObjectLoader | None = None, |
| 77 | + nowait: bool = False, |
| 78 | + no_reply: bool = False, |
| 79 | + ) -> ProcessResult: |
| 80 | + """ |
| 81 | + Launch a process given the class and constructor arguments |
| 82 | +
|
| 83 | + :param process_class: the class of the process to launch |
| 84 | + :param init_args: the constructor positional arguments |
| 85 | + :param init_kwargs: the constructor keyword arguments |
| 86 | + :param persist: should the process be persisted |
| 87 | + :param loader: the classloader to use |
| 88 | + :param nowait: if True, don't wait for the process to send a response, just return the pid |
| 89 | + :param no_reply: if True, this call will be fire-and-forget, i.e. no return value |
| 90 | + :return: the result of launching the process |
| 91 | + """ |
| 92 | + ... |
| 93 | + |
| 94 | + async def execute_process( |
| 95 | + self, |
| 96 | + process_class: str, |
| 97 | + init_args: Sequence[Any] | None = None, |
| 98 | + init_kwargs: dict[str, Any] | None = None, |
| 99 | + loader: loaders.ObjectLoader | None = None, |
| 100 | + nowait: bool = False, |
| 101 | + no_reply: bool = False, |
| 102 | + ) -> ProcessResult: |
| 103 | + """ |
| 104 | + Execute a process. This call will first send a create task and then a continue task over |
| 105 | + the communicator. This means that if communicator messages are durable then the process |
| 106 | + will run until the end even if this interpreter instance ceases to exist. |
| 107 | +
|
| 108 | + :param process_class: the process class to execute |
| 109 | + :param init_args: the positional arguments to the class constructor |
| 110 | + :param init_kwargs: the keyword arguments to the class constructor |
| 111 | + :param loader: the class loader to use |
| 112 | + :param nowait: if True, don't wait for the process to send a response |
| 113 | + :param no_reply: if True, this call will be fire-and-forget, i.e. no return value |
| 114 | + :return: the result of executing the process |
| 115 | + """ |
| 116 | + ... |
0 commit comments