Remote Procedure Calls with kaa.rpc

This module defines an alternative way for InterProcessCommunication with less features than the ipc.py module. It does not keep references, return values are only given back as a callback and it is only possible to access functions. The difference between client and server is only important on connect. Both can call functions for the other.

Start a server:

kaa.rpc.Server(address, secret)

When a new client connects to the server, the ‘client_connected’ signals will be emitted with a Channel object as parameter. This object can be used to call functions on client side the same way the client calls functions on server side. The client and the channel objects have a signal ‘disconnected’ to be called when the connection gets lost.

Start a client:

kaa.rpc.Client(address, secret)

Since everything is async, the challenge response is done in the background and you can start using it right away. If the authentication is wrong, it will fail without notifing the user (I know this is bad, but it is designed to work internaly where everything is correct).

class kaa.rpc.Server(address, auth_secret='', buffer_size=None)

RPC server class. RPC servers accept incoming connections from client, however RPC calls can be issued in either direction.

address specifies what address to bind the socket to, and this argument must correspond to the bind_info argument of kaa.Socket.listen().

See kaa.Socket.buffer_size docstring for information on buffer_size.

Synopsis

Class Hierarchy

kaa.Object
└─ kaa.rpc.Server

Methods
close()Close the server socket.
disconnect()Disconnects a previously connected object.
register()Registers one or more previously exposed callables to any clients connecting to this RPC Server.
Properties
This class has no properties.
Signals
client-connectedEmitted when a new RPC client connects to this RPC server.

Methods

close()

Close the server socket.

disconnect(obj)

Disconnects a previously connected object.

register(obj)

Registers one or more previously exposed callables to any clients connecting to this RPC Server.

Parameters:obj (callable, module, or class instance) – callable(s) to be accessible to connected clients.

If a module is given, all exposed callables.

Signals

client-connected

Emitted when a new RPC client connects to this RPC server.

def callback(client, ...)
Param client:the new client that just connected
Type client:Client object

class kaa.rpc.Client(address, auth_secret='', buffer_size=None, retry=None)

RPC client to be connected to a server.

Synopsis

Class Hierarchy

kaa.Object
└─ kaa.rpc.Channel
     └─ kaa.rpc.Client

Methods
close()Forcefully close the RPC channel.
register()Registers one or more previously exposed callables to the peer
rpc()Call the remote command and return InProgress.
Properties
connectedread-only
Signals
openEmitted when the RPC channel has successfully authenticated.
closedEmitted when the RPC channel is closed.

Methods

close()

Forcefully close the RPC channel.

register(obj)

Registers one or more previously exposed callables to the peer

Parameters:obj (callable, module, or class instance) – callable(s) to be accessible to.

If a module is given, all exposed callables.

rpc(cmd, *args, **kwargs)

Call the remote command and return InProgress.

Properties

connected

Signals

open

Emitted when the RPC channel has successfully authenticated.

def callback(...)
closed

Emitted when the RPC channel is closed.

def callback(...)

Expose Functions

Next you need to define functions the remote side is allowed to call and give it a name. Use use expose for that. Connect the object with that function to the server/client. You can connect as many objects as you want. The client can now call do_something (not my_function, this is the internal name). If the internal name should be exposed the expose decorator does not need its first argument:

class MyClass(object)

    @kaa.rpc.expose("do_something")
    def my_function(self, foo):
        ...

    @kaa.rpc.expose()
    def name(self, foo):
        ...

server.connect(MyClass())
kaa.rpc.expose(command=None, add_client=False, coroutine=False)

Decorator to expose a function. If add_client is True, the client object will be added to the command list as first argument.

Calling Remote Functions

A remote function call be called with the rpc method in the client or the server. The result is an InProgress object. Connect to it to get the result:

x = client.rpc('do_something', 6) or
x = client.rpc('do_something', foo=4)

Using Python 2.5 and coroutines the asynchronous call can be wrapped in a yield statement, hiding the delay of the RPC. If the server raises an exception, it will be raised on client side. This makes remote functions look like local functions. Note: Python will jump back to the mainloop in each yield:

@kaa.coroutine()
def foo():
    name = yield client.rpc('name')
    print name

Table Of Contents

Previous topic

Sub-Process I/O

Next topic

Configuration Files

This Page