From 91fc5eec68a6c8d570c55596a95958916ff27b98 Mon Sep 17 00:00:00 2001 From: Antoine Millet Date: Tue, 4 Oct 2011 18:19:59 +0200 Subject: [PATCH] Moved callers to sjrpc.core.protocols.rpc module --- sjrpc/core/callers.py | 75 ------------------------------------ sjrpc/core/protocols/rpc.py | 76 ++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 77 deletions(-) delete mode 100644 sjrpc/core/callers.py diff --git a/sjrpc/core/callers.py b/sjrpc/core/callers.py deleted file mode 100644 index e837edf..0000000 --- a/sjrpc/core/callers.py +++ /dev/null @@ -1,75 +0,0 @@ -import threading - -from sjrpc.core.exceptions import RpcError - -ERRMSG_RPCERR = ('Unable to send reply to the peer: %s (this error is usualy ' - 'raised when connection is lost while handler function ' - 'execution)') - - -class RpcCaller(object): - ''' - A caller execute a callable (function, method, class which implement the - :meth:`__call__` method...) in a particular context (threaded or - "timeouted" for example), and return the result (or the exception) to the - peer through it :class:`RpcConnection` object. - ''' - - def __init__(self, request, protocol, func): - self._request = request - self._protocol = protocol - self._func = func - - # Apply the request decorator - #request_decorator = connection.request_decorator - #if request_decorator is not None: - # self._func = request_decorator(self._func) - - def run(self): - ''' - Run the callable and return the result (or the exception) to the peer. - ''' - msg_id = self._request['id'] - args = self._request['args'] - kwargs = self._request['kwargs'] - - if getattr(self._func, '__pass_rpc__', False): - args.insert(0, self._protocol) - if getattr(self._func, '__pass_connection__', False): - args.insert(0, self._protocol.connection) - try: - returned = self._func(*args, **kwargs) - except Exception as err: - try: - self._protocol.error(msg_id, message=str(err), - error=err.__class__.__name__) - except RpcError as err: - self._protocol.connection.logger.error(ERRMSG_RPCERR, err) - else: - try: - self._protocol.response(msg_id, returned=returned) - except RpcError as err: - self._protocol.connection.logger.error(ERRMSG_RPCERR, err) - - def start(self): - ''' - Start execution of the callable, the most of time, it just call - :meth:`run` method. - ''' - self.run() - - -class ThreadedRpcCaller(RpcCaller): - - ''' - A caller which make the call into a separated thread. - ''' - - def __init__(self, *args, **kwargs): - super(ThreadedRpcCaller, self).__init__(*args, **kwargs) - self._thread = threading.Thread(target=self.run) - self._thread.name = 'Processing of call: %s' % self._request['id'] - self._thread.daemon = True - - def start(self): - self._thread.start() diff --git a/sjrpc/core/protocols/rpc.py b/sjrpc/core/protocols/rpc.py index 2dda4e5..99671c0 100644 --- a/sjrpc/core/protocols/rpc.py +++ b/sjrpc/core/protocols/rpc.py @@ -3,14 +3,86 @@ from __future__ import absolute_import import json from uuid import uuid4 -from threading import Event +from threading import Event, Thread -from sjrpc.core.callers import RpcCaller, ThreadedRpcCaller from sjrpc.core.exceptions import RpcError from sjrpc.core.protocols import Protocol __all__ = ['RpcProtocol'] +ERRMSG_RPCERR = ('Unable to send reply to the peer: %s (this error is usualy ' + 'raised when connection is lost while handler function ' + 'execution)') + + +class RpcCaller(object): + ''' + A caller execute a callable (function, method, class which implement the + :meth:`__call__` method...) in a particular context (threaded or + "timeouted" for example), and return the result (or the exception) to the + peer through it :class:`RpcConnection` object. + ''' + + def __init__(self, request, protocol, func): + self._request = request + self._protocol = protocol + self._func = func + + # Apply the request decorator + #request_decorator = connection.request_decorator + #if request_decorator is not None: + # self._func = request_decorator(self._func) + + def run(self): + ''' + Run the callable and return the result (or the exception) to the peer. + ''' + msg_id = self._request['id'] + args = self._request['args'] + kwargs = self._request['kwargs'] + + if getattr(self._func, '__pass_rpc__', False): + args.insert(0, self._protocol) + if getattr(self._func, '__pass_connection__', False): + args.insert(0, self._protocol.connection) + try: + returned = self._func(*args, **kwargs) + except Exception as err: + try: + self._protocol.error(msg_id, message=str(err), + error=err.__class__.__name__) + except RpcError as err: + self._protocol.connection.logger.error(ERRMSG_RPCERR, err) + else: + try: + self._protocol.response(msg_id, returned=returned) + except RpcError as err: + self._protocol.connection.logger.error(ERRMSG_RPCERR, err) + + def start(self): + ''' + Start execution of the callable, the most of time, it just call + :meth:`run` method. + ''' + self.run() + + +class ThreadedRpcCaller(RpcCaller): + + ''' + A caller which make the call into a separated thread. + ''' + + def __init__(self, *args, **kwargs): + super(ThreadedRpcCaller, self).__init__(*args, **kwargs) + self._thread = Thread(target=self.run) + self._thread.name = 'Processing of call: %s' % self._request['id'] + self._thread.daemon = True + + def start(self): + self._thread.start() + + class RpcProtocol(Protocol): ''' -- GitLab