Basically a service is just an object. But a service can also be executed in a separate process or thread.
Just derive a class from Service and it’s done.
class TestService(Service):
def do_something(self, *args, **kwargs)
# do some stuff
There are some class attributes which can be used to adjust the service behaviour.
As you can see in Use a Service, support for the with statement is provided. The downside is, everytime using with the constructor is called. There may be some initialization stuff that is only necessary when creating that service in another process or thread. For that purpose init_resources() can be used as well as close_resources() to clean up everything before the process gets stopped.
For example a service that needs a file:
class FileService(Service):
def init_resources(self):
super(FileService, self).init_resources()
# open file
self._file = open('..')
def close_resources(self):
super(FileService, self).close_resources()
# close file
self._file.close()
def do_something(self, *args, **kwargs)
# do some stuff
The easiest way to use a service is the with-statement:
with TestService() as service:
service.do_something()
Note
The process that starts a service is the one that manages it.
Another way is to start and stop the service as well as connecting to the service manually:
TestService.start()
# ...
with TestService.get_connection() as service:
service.do_something()
# ...
TestService.stop()
You can either get a synchronous or asynchronous connection which is controlled by the async parameter of the constructor or of get_connection().
Note
A Service can be used as described above, in combination with QueuedService or as a normal Python object. But in the latter case you have to care that init_resources() and close_resources() are called, if necessary. (See Local Service Example)
A synchronous call is a blocking call like a normal function call. If you have an asynchronous connection, it is also possible to do synchronous calls using the sync attribute.
with TestService.get_connection(async=False) as service:
service.do_something()
with TestService.get_connection(async=True) as service:
service.sync.do_something()
An asynchronous call is a not blocking call and returns a Pyro4.futures.FutureResult object. If you have a synchronous connection, it is also possible to do asynchronous calls using the async attribute:
with TestService.get_connection(async=True) as service:
service.do_something()
with TestService.get_connection(async=False) as service:
service.async.do_something()
Warning
Take care that all asynchronous calls are finished before you stop a service or they will not processed and raise a timeout error.
Pyro4 has a feature called Autoproxying. This feature is enabled for PyroMP services. If a running Service object is passed to another process. It is replaced by a Proxy to it.
Base class for all services
Parameters : | multiplex : bool
async : bool
|
---|
Methods
Returns the logger of this class or just a logger stub, if LOGGING is False
Called when the service process is starting after everything else was set up
Called before stopping the service process
Class Methods
Parameters : | async : bool
|
---|
Returns a name for registering at a NameServer
Checks if the Service is running
Returns : | running : bool
|
---|
Starts the service if not already running
Parameters : | multiplex : bool
|
---|
Notes
Only the process that has started a service is able to stop it.
Stops the service if running and managed by this process
Manages an Proxy for the given service
Notes
Do not use the constructor manually, use Service.get_connection() instead
with-statement can be used to connect and disconnect, too
Methods
Connects to the service
Raises : | exc : NameServerError
exc : ServiceNotAvailableError
|
---|
Releases the connection to the service