Callbacks

Callbacks to a Service

Derive the class from CallbackService instead of Service to support callback functionality. All methods decorated with callback are wrapped by CallbackFunction.

Here is an example:

class TestService(CallbackService):

    @callback
    def callback_function(self, *args, **kwargs):
        # do some stuff

Note

For an extensive example see Callback Service Example.

Warning

It is not possible to start a CallbackService in multiplex mode, because that would lead to deadlocks.

Callbacks to a process/thread that is no Service

To perform the communication with Pyro4 there must be a CallbackServer running. Use the with-statement:

with CallbackServer():
    # do some stuff

Or start and stop the server manually:

CallbackServer.start()
# do some stuff
CallbackServer.stop()

Classes which should provide callback functionality have to be derived from CallbackObject and the callback functions have to be decorated with callback to be wrapped by CallbackFunction.

Here is an example:

class TestObject(CallbackObject):

    @callback
    def callback_function(self, *args, **kwargs):
        # do some stuff

Note

For an extensive example see Callback Object Example.

Using callbacks

Just call call(). See also Callback Object Example or Queued Callback Example..

Events

Events join several callbacks together. To get callbacks from that event you just have to register():

_images/cb_register.png

If trigger() is called, the call is forwarded to all registered callbacks:

_images/event_trigger.png

The caller doesn’t have to care how many callbacks are registered. Doesn’t matter if there are no registered callbacks at all.

Note

Each callback is executed in its own thread and errors are logged.

API Reference

@PyroMP.callback

Marks class method as callback function


class PyroMP.CallbackFunction(func, name, daemon=None)

Wraps a function and registers at the CallbackServer or the given Pyro4.Daemon.

Parameters :

func : callable

Function to be wrapped

daemon : Pyro4.Daemon

(optional) if provided, registered at that Daemon instead of registering at the CallbackServer

Notes

Can be called like normal functions

Callbacks are called using autoproxy feature of Pyro4. As Pyro4 does only support function calls, an additional call() function is provided.

Methods

call(*args, **kwargs)

Calls the wrapped function with given args and kwargs.

id()

Returns a unique id for this callback


class PyroMP.CallbackObject(**kwargs)

Wraps all methods decorated with callback with a CallbackFunction


class PyroMP.CallbackServer

Manages a thread with a daemon to serve callbacks

Implements the singleton pattern.

Static Methods

classmethod register(cb_func)

Register the given CallbackFunction at the server

Parameters :

cb_func : CallbackFunction

Function to be registered

classmethod start()

Starts the server

Raises :

exc : CallbackServerAlreadyRunningError

If the server is already running

classmethod stop()

Stops the server

Raises :

exc : CallbackServerNotRunningError

If the server is not running

classmethod unregister(cb_func)

Register the given CallbackFunction at the server

Parameters :

cb_func : CallbackFunction

Function to be registered


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

Bases: Service

Service that wraps all methods decorated with callback with a CallbackFunction

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


class PyroMP.Event

Methods

register(callback)

Adds the callback to this event

unregister(callback)

Removes the callback from this event

trigger(*args, **kwargs)

Calls all registered callback functions. The args and kwargs are forwarded to each of them

Table Of Contents

Previous topic

Basic Services

Next topic

Queued Services

This Page