msl.network.service module
Base class for all Services.
- class msl.network.service.Service(*, name=None, max_clients=None, ignore_attributes=None)[source]
Bases:
Device
Base class for all Services.
New in version 0.4: The name and max_clients keyword argument.
New in version 0.5: The ignore_attributes keyword argument.
New in version 1.0: If a method of the Service returns an object that is not natively JSON serializable, then the returned object can have a callable
to_json()
method and the value returned byto_json()
will be used in the response to theClient
.- Parameters:
name (
str
, optional) – The name of the Service as it will appear on the NetworkManager
. If not specified then the class name is used. You can also specify the name in thestart()
method.max_clients (
int
, optional) – The maximum number ofClient
s that can be linked with this Service. A value \(\leq\) 0 orNone
means that there is no limit.ignore_attributes (
str
orlist
ofstr
, optional) – The names of the attributes to not include in theidentity
of the Service. Seeignore_attributes()
for more details.
- property max_clients
The maximum number of
Client
s that can be linked with thisService
. A value \(\leq\) 0 means an unlimited number ofClient
s can be linked.- Type:
- emit_notification(*args, **kwargs)[source]
Emit a notification to all
Client
s that areLink
ed with thisService
.New in version 0.5.
- Parameters:
args – The arguments to emit.
kwargs – The keyword arguments to emit.
- emit_notification_threadsafe(*args, **kwargs)[source]
A thread-safe implementation of
emit_notification()
.When a
Service
handles a request, it does so in a separate thread than the event loop is running in. Therefore, if a method of theService
class wants to emit a notification while it is handling a request then it must emit the notification in a thread-safe manner.New in version 1.0.
- Parameters:
args – The arguments to emit.
kwargs – The keyword arguments to emit.
See also
- ignore_attributes(*names)[source]
Ignore attributes from being added to the
identity
of theService
.There are a few reasons why you may want to call this method:
If you see warnings that an object is not JSON serializable or that the signature of an attribute cannot be found when starting the
Service
and you prefer not to see the warnings.If you do not want an attribute to be made publicly known that it exists. However, a
Client
can still access the ignored attributes.
Private attributes (i.e., attributes that start with an underscore) are automatically ignored and cannot be accessed from a
Client
on the network.If you want to ignore any attributes then you must call
ignore_attributes()
before callingstart()
.New in version 0.5.
- start(*, name=None, host='localhost', port=1875, timeout=10, username=None, password=None, password_manager=None, read_limit=None, disable_tls=False, cert_file=None, assert_hostname=True, auto_save=False)[source]
Start the
Service
.See
connect()
for the description of each parameter.
- property request
Returns the latest request.
This property is meant to be used by a subclass that may want to know the information about the request while processing it.
Since a request is executed in a separate thread and this property returns the latest request, the subclass should immediately extract the necessary information from the request before the
Service
receives a new request.The key-value pairs in the request are:
{ 'args': list, 'attribute': str, 'kwargs': dict, 'service': str (the name of this Service), 'uid': str, 'requester': str, }
New in version 1.1.
- Type: