Skip to content

ding.interaction

ding.interaction

Master

Bases: ControllableService

Overview

Interaction master end

my_address property

Overview

Get my address property of current master client.

Returns: - output (:obj:str): My address which can be used to establish connection from slave end to here.

__init__(host=None, port=None, heartbeat_tolerance=None, heartbeat_check_span=None, request_retries=None, request_retry_waiting=None, channel=None, my_address=None)

Overview

Constructor of Master

Arguments: - host (:obj:Optional[str]): Host of the master server, based on flask (None means 0.0.0.0) - port (:obj:Optional[int]): Port of the master server, based on flask (None means 7235) - heartbeat_tolerance: (:obj:Optional[float]): Max time tolerance of the heartbeat missing (None means 15.0, minimum is 0.2, unit: second) - heartbeat_check_span: (:obj:Optional[float]): Timespan between the heartbeat status check (None means 1.0, minimum is 0.1, unit: second) - request_retries (:obj:Optional[int]): Max times for request retries (None means 5) - request_retry_waiting (:obj:Optional[float]): Sleep time before requests' retrying (None means 1.0, unit: second) - channel (:obj:Optional[int]): Channel id for the master client, please make sure that channel id is equal to the slave client's channel id, or the connection cannot be established. (None means 0, but 0 channel is not recommended to be used in production) - my_address (:obj:Optional[str]): The address of current server (None will grep local ip automatically, this address will be used when connect to slave, the slave's request will be send to this address, so please make sure the address can be achieved by slave)

ping()

Overview

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

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

new_connection(name, host, port=None, https=False)

Overview

Create a new connection object to slave end (but the connection will be established immediately before connect method in connection object is called).

Arguments: - name (:obj:str): Name of the connection (this name is an unique label used in this master client) - host (:obj:str): Host of the slave end - port (:obj:Optional[int]): Port of the slave end (None means 7236) - https (:obj:bool): Use https to connect or not (Default is False) Returns: - output (:obj:SlaveConnectionProxy): A connection object represents the connection from here to the slave end. More actions can be operated by this connection object.

__contains__(name)

Overview

Check if the active connection with the given name exist in this master client. Only connections still alive can be found here.

Arguments: - name (:obj:str): Name of the connection Returns: - output (:obj:bool): Whether connection with the given name exist.

__getitem__(name)

Overview

Try get the active connection with the given name. Only connections still alive can be found here.

Arguments: - name (:obj:str): Name of the connection Returns: - output (:obj:bool): Connection object with the given name.

__delitem__(name)

Overview

Delete connection from this master client, and the deleted connection will be killed as well. Only connections still alive can be found here.

Arguments: - name (:obj:str): Name of the connection

start()

Overview

Start current master client Here are the steps executed inside in order: 1. Start the result-processing thread 2. Start the heartbeat check thread 3. Start the http server thread 4. Wait until the http server is online (can be pinged)

shutdown()

Overview

Shutdown current master 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).

join()

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 check thread down 3. Wait until the result-processing thread down

TaskRefuse

Bases: ResponsibleException

Overview

Exception represents the refuse to tasks, can be used in method _before_task.

Example: - Without data

>>> raise TaskRefuse

- With refuse data

>>> raise TaskRefuse({'data': 233})

__init__(data=None)

Overview

Constructor of TaskRefuse

Arguments: - data (:obj:Optional[Mapping[str, Any]]): Key-value-formed refuse data

DisconnectionRefuse

Bases: ResponsibleException

Overview

Exception represents the refuse to disconnection to slave from master, can be used in method _before_disconnection.

Example: - Without data

>>> raise DisconnectionRefuse

- With refuse data

>>> raise DisconnectionRefuse({'data': 233})

__init__(data=None)

Overview

Constructor of DisconnectionRefuse

Arguments: - data (:obj:Optional[Mapping[str, Any]]): Key-value-formed refuse data

ConnectionRefuse

Bases: ResponsibleException

Overview

Exception represents the refuse to connection to slave from master, can be used in method _before_connection.

Example: - Without data

>>> raise ConnectionRefuse

- With refuse data

>>> raise ConnectionRefuse({'data': 233})

__init__(data=None)

Overview

Constructor of ConnectionRefuse

Arguments: - data (:obj:Optional[Mapping[str, Any]]): Key-value-formed refuse data

TaskFail

Bases: Exception

Overview

Exception represents the failure of tasks, can be used in method _process_task.

Example: - Without data

>>> raise TaskFail

- With failure data

>>> raise TaskFail({'data': 233})

- With both data and message

>>> raise TaskFail({'data': 233}, 'this is message')

result property

Overview

Get the result of task failure.

Returns: Result of task failure.

__init__(result=None, message=None)

Overview

Constructor of TaskFail

Arguments: - result (:obj:Optional[Mapping[str, Any]]): Result of task failure - message (:obj:Optional[str]): Message of task failure

Slave

Bases: ControllableService

Overview

Interaction slave client

__init__(host=None, port=None, heartbeat_span=None, request_retries=None, request_retry_waiting=None, channel=None)

Overview

Constructor of Slave class

Arguments: - host (:obj:Optional[str]): Host of the slave server, based on flask (None means 0.0.0.0) - port (:obj:Optional[int]): Port of the slave server, based on flask (None means 7236) - heartbeat_span (:obj:Optional[float]): Time span of heartbeat packages in seconds (None means 3.0, minimum is 0.2) - request_retries (:obj:Optional[int]): Max times for request retries (None means 5) - request_retry_waiting (:obj:Optional[float]): Sleep time before requests' retrying (None means 1.0) - channel (:obj: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)

ping()

Overview

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

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

start()

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)

shutdown()

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).

join()

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

Full Source Code

../ding/interaction/__init__.py

1from .master import * 2from .slave import *