Python 7.0pl5 Benutzerhandbuch

Seite von 124
Service Reports
50
class Subscriber(netsvc.Service):
def __init__(self):
netsvc.Service.__init__(self)
# subscribe to the service group "publishers"
self.subscribeServiceGroup(self.announce,"publishers")
def announce(self,binding,group,status):
if status == netsvc.SERVICE_AVAILABLE:
# now subscribe to service agent which is member of group
self.monitorReports(self.report,binding,"system.*")
else:
self.ignoreReports(binding)
def report(self,service,subject,content):
binding = self.currentReport().publisher()
identity = binding.agentIdentity()
publisher = "(%s/%s)" % (‘service‘,identity)
if subject == "system.ctime":
now = str(netsvc.DateTime())
print "%s became available at %s" % (publisher,now)
print "%s originally started at %s" % (publisher,str(content))
elif subject == "system.time":
print "%s was still alive at %s" % (publisher,str(content))
As expected, the subject is that under which any report was published. As to the content of the report,
this is not limited to being a string, but can be any of the basic Python scalar types, a list, tuple or dic-
tionary, as well as the
None
type and a number of extended types. User defined scalar types can also
be used providing that appropriate encoders/decoders are available.
If you wish to cancel a subscription to a service, the "
ignoreReports()
" member function should
be used. This should be supplied the name of the service and the exact same subject pattern used when
subscribing to the reports in the first place. If no subject pattern is supplied, all subscriptions against
that service name will be removed.
Lifetime of Reports
When publishing a report, the report will be sent to any service agents which have a current subscrip-
tion which matches the subject associated with the report. The default behaviour is then such that the
publishing service forgets all about the report. In this case, if a new subscription arrived immediately
after, it would only be sent any reports which were published after its subscription was received. The
new subscriber would not receive a copy of the report which was published just before its subscription
was received.
In some situations however, it is desirable that a new subscriber be able to obtain the last report which
may have been published against any subject it is interested in. This is useful in the context that a report
is used to reflect the status of a service. By being able to obtain the last published report, a subscriber
can know the current state of the service immediately and doesn’t have to explicitly request it or wait
for the status to change.