Basic Services

What is a Service?

Basically a service is just an object. But a service can also be executed in a separate process or thread.

Define a Service

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.

  • Service.LOGGING: Enable/disable logging (default: True)
  • Service.THREADED: If True a thread is started instead of a process (default: False)
  • Service.DAEMON: Flag is forwarded to the deamon flag of the created Process or Thread (default: True)

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

Use a Service

The easiest way to use a service is the with-statement:

with TestService() as service:
    service.do_something()
  • If the service is not running the service is started and stopped automatically after the with-block.
  • If the service is running and managed by the current process, it is not started again but stopped after the with-block.
  • If the service is running and managed by another process, only a connection is initialized and released after the with-block. The service continues running.

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)

Synchronous

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

Asynchronous

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.

Autoproxy

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.

API Reference

class PyroMP.Service(multiplex=False, async=True, **kwargs)

Base class for all services

Parameters :

multiplex : bool

Determines the servertype when using with
  • True -> multiplex
  • False -> threaded

See Pyro4 documentation

async : bool

Determines if the connection returned using with is synchronous or asynchronous

Methods

get_logger()

Returns the logger of this class or just a logger stub, if LOGGING is False

init_resources()

Called when the service process is starting after everything else was set up

close_resources()

Called before stopping the service process

Class Methods

classmethod get_connection(async=True)
Parameters :

async : bool

Determines if the connection is synchronous or asynchronous

classmethod get_pyro_name()

Returns a name for registering at a NameServer

classmethod is_running()

Checks if the Service is running

Returns :

running : bool

True if it is running False otherwise

classmethod start(multiplex=False)

Starts the service if not already running

Parameters :

multiplex : bool

Determines the servertype.
  • True -> multiplex
  • False -> threaded

See Pyro4 documentation

Notes

Only the process that has started a service is able to stop it.

classmethod stop()

Stops the service if running and managed by this process


class PyroMP.ServiceConnection(service, async)

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

connect()

Connects to the service

Raises :

exc : NameServerError

If no Nameserver is running

exc : ServiceNotAvailableError

If the service is not running

disconnect()

Releases the connection to the service

Table Of Contents

Previous topic

Basic Concept and Environment

Next topic

Callbacks

This Page