From 4a764643b2baaa07ff89845c3b5c3ee332437094 Mon Sep 17 00:00:00 2001 From: Antoine Millet Date: Wed, 26 Jan 2011 16:00:14 +0100 Subject: [PATCH] Added timeout feature on (remote) call method. --- sjrpc/core/rpcconnection.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sjrpc/core/rpcconnection.py b/sjrpc/core/rpcconnection.py index d1e28fd..de1d35c 100644 --- a/sjrpc/core/rpcconnection.py +++ b/sjrpc/core/rpcconnection.py @@ -211,7 +211,7 @@ class RpcConnection(object): err = {'exception': error, 'message': message} self._send_response(msg_id, error=err) - def call(self, method_name, *args, **kwargs): #TODO: timeout + def call(self, method_name, *args, **kwargs): ''' Make a new remote call on the peer. @@ -226,6 +226,12 @@ class RpcConnection(object): seconds before to raise an :exc:`CallTimeout` exception if the peer didnt respond. ''' + + if '_timeout' in kwargs: + timeout = kwargs['_timeout'] + del kwargs['_timeout'] + else: + timeout = None # Send the call to the peer: msg_id = self._send_call(method_name, *args, **kwargs) @@ -235,7 +241,11 @@ class RpcConnection(object): 'event': threading.Event()} # Wait for the response: - self._calls[msg_id]['event'].wait() + self._calls[msg_id]['event'].wait(timeout) + + # Check if timeout occured: + if not self._calls[msg_id]['event'].is_set(): + raise RpcError('TimeoutError', 'remote method timeout') # Check if error occured while execution: if self._calls[msg_id]['error'] is not None: -- GitLab