Commit 335267c9 authored by Anael Beutot's avatar Anael Beutot Committed by Antoine Millet
Browse files

Async call with callback

parent 32d5bfea
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -162,6 +162,10 @@ class RpcProtocol(Protocol):
            if 'event' in call:
                # Release the call if its synchronous:
                call['event'].set()
            elif 'async_cb' in call:
                call['async_cb'](call_id=message['id'],
                                 response=call.get('return'),
                                 error=call.get('error'))
            else:
                # Else, it's an asynchonous call, we need to push the answer
                # on the queue:
@@ -420,3 +424,21 @@ class RpcProtocol(Protocol):
        self._calls[msg_id] = {'id': msg_id, 'async': True, 
                               'data': data, 'queue': queue}
        return msg_id

    def async_call_cb(self, callback, method_name, *args, **kwargs):
        '''Make an asynchronous call on the peer and callback when call is done.

        :param callable callback: callback must have the following named
            arguments:

            * **call_id** (*int*) - unique identifier for the async call made
            * **response** - response as returned by the remote function
            * **error** (*dict*) - error indicator in case remote call throwed exception
        :param str method_name: the method to call on the peer
        :param \*args: the arguments for the call
        :param \*\*kwargs: the keyword arguments for the call
        :return: the message id of the call
        '''
        msg_id = self._send_call(method_name, *args, **kwargs)
        self._calls[msg_id] = {'id': msg_id, 'async_cb': callback}
        return msg_id