Python 7.0pl5 Benutzerhandbuch

Seite von 124
Service Audience
39
Service Audience
When you create a service, the existance of that service will be broadcast to all connected processes.
If you wish to restrict visibility of a service to just the process the service is contained in, or a subset
of the connected processes, a service audience can be defined.
To define the service audience, an extra argument needs to be supplied to the
Service
base class
when it is initialised. By default the service audience is "
*
" to indicate that knowledge of the service
should be broadcast as widely as possible. Setting the service audience to an empty string, will restrict
visibility of the service to the local process.
class AlarmMonitor(netsvc.Service):
def __init__(self,name="alarm-monitor",audience="*"):
netsvc.Service.__init__(self,name,audience)
Other values can be supplied for the service audience and their meaning will depend upon how the in-
terprocess communications links of the service agent framework are configured. This aspect of the
service audience field will be discussed when support for distributed applications is covered.
Note that in setting the service audience, you are also restricting your service agent as far as what serv-
ices it can subscribe to. If you set the service audience to that indicating the local process only, you
will only be able to subscribe to services which exist in the local process. This is because services in
remote processes will not know anything about you. If you need to be able to subscribe to services no
matter where they are, you would generally be best leaving the service audience set to the default value.
Anonymous Service
Although referred to as a service, a service agent can act in the role of either a client or server. That is,
as a client it is a user of other services and would not expect to have subscriptions made against it or
receive requests. In this situation the name assigned to the service is immaterial and it is valid to supply
an empty service name. In fact, if you do not explicitly supply a service name when initialising the
Service
base class, it will default to an empty string.
class AnonymousService(netsvc.Service):
def __init__(self):
netsvc.Service.__init__(self)
In general it is still preferable to supply a non empty value for the service name. Doing so will mean
that the service agent will appear as a separate entity within any debugging tools and although the ap-
plication itself may not need to use that service agent in the role of a server, you might still include
functionality which can be used from debugging tools so you know what the service agent is doing.