interaction.slave.slave

Slave

class ding.interaction.slave.slave.Slave(host: Optional[str] = None, port: Optional[int] = None, heartbeat_span: Optional[float] = None, request_retries: Optional[int] = None, request_retry_waiting: Optional[float] = None, channel: Optional[int] = None)[source]
Overview:

Interaction slave client

__init__(host: Optional[str] = None, port: Optional[int] = None, heartbeat_span: Optional[float] = None, request_retries: Optional[int] = None, request_retry_waiting: Optional[float] = None, channel: Optional[int] = None)[source]
Overview:

Constructor of Slave class

Arguments:
  • host (Optional[str]): Host of the slave server, based on flask (None means 0.0.0.0)

  • port (Optional[int]): Port of the slave server, based on flask (None means 7236)

  • heartbeat_span (Optional[float]): Time span of heartbeat packages in seconds (None means 3.0, minimum is 0.2)

  • request_retries (Optional[int]): Max times for request retries (None means 5)

  • request_retry_waiting (Optional[float]): Sleep time before requests’ retrying (None means 1.0)

  • channel (Optional[int]): Channel id for the slave client, please make sure that channel id is equal to the master client’s channel id, or the connection cannot be established. (None means 0, but 0 channel is not recommended to be used in production)

_before_connection(data: Mapping[str, Any])[source]
Overview:

Behaviours that will be executed before connection is established.

Arguments:
  • data (Mapping[str, Any]): Connection data when connect to this slave, sent from master.

Raises:
  • ConnectionRefuse After raise this, the connection from master end will be refused, no new connection will be established.

_before_disconnection(data: Mapping[str, Any])[source]
Overview:

Behaviours that will be executed before disconnection is executed.

Arguments:
  • data (Mapping[str, Any]): Disconnection data when disconnect with this slave, sent from master.

Raises:
  • DisconnectionRefuse After raise this, the disconnection request will be refused, current connection will be still exist.

_before_task(data: Mapping[str, Any])[source]
Overview:

Behaviours that will be executed before task is executed.

Arguments:
  • data (Mapping[str, Any]): Data of the task

Raises:
  • TaskRefuse After raise this, the new task will be refused.

_lost_connection(master_address: str, err: requests.exceptions.RequestException)[source]
Overview:

Behaviours that will be executed after connection is lost.

Arguments:
  • master_address (str): String address of master end

  • err (request.exceptions.RequestException): Http exception of this connection loss

abstract _process_task(task: Mapping[str, Any])[source]
Overview:

Execute the task, this protected method must be implement in the subclass.

Arguments:
  • task (Mapping[str, Any]): Data of the task

Raises:
  • TaskFail After raise this, this task will be recognized as run failed, master will received the failure signal.

Example:
  • A success task with return value (the return value will be received in master end)

>>> def _process_task(self, task):
>>>     print('this is task data :', task)
>>>     return str(task)
  • A failed task with data (the data will be received in master end)

>>> def _process_task(self, task):
>>>     print('this is task data :', task)
>>>     raise TaskFail(task)  # this is a failed task
  • A failed task with data and message (both will be received in master end)

>>> def _process_task(self, task):
>>>     print('this is task data :', task)
>>>     raise TaskFail(task, 'this is message')  # this is a failed task with message
join()[source]
Overview:

Wait until current slave client is down completely. Here are the steps executed inside in order:

  1. Wait until the http server thread down

  2. Wait until the heartbeat thread down

  3. Wait until the task-processing thread down

ping() bool[source]
Overview:

Ping the current http server, check if it still run properly.

Returns:
  • output (bool): The http server run properly or not. True means run properly, otherwise return False.

shutdown()[source]
Overview:

Shutdown current slave client. A shutdown request will be sent to the http server, and the shutdown signal will be apply into the threads, the server will be down soon (You can use join method to wait until that time).

start()[source]
Overview:

Start current slave client Here are the steps executed inside in order:

  1. Start the task-processing thread

  2. Start the heartbeat thread

  3. Start the http server thread

  4. Wait until the http server is online (can be pinged)