Commit 4eadc7b5 authored by Antoine Millet's avatar Antoine Millet
Browse files

Rewrited sjRpc with low level channel multiplexage and gevent usage.

parent d63729bd
Loading
Loading
Loading
Loading
+1 −5
Changes for sjrpc/__init__.py: 1 added line, 5 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/env python
#coding:utf8

'''

@@ -11,14 +9,12 @@ The library is separated into four parts:

 * core library contains all common classes.
 * server library contains all the server side related stuff.
 * client library contains all the client side related stuff.
 * utils library contains some helpers used in previous libraries.

'''

import sjrpc.core
import sjrpc.server
import sjrpc.client
import sjrpc.utils

__version__ = 13
__version__ = '14~dev'
−6

File deleted.

−109

File deleted.

+3 −4
Changes for sjrpc/core/__init__.py: 3 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -2,10 +2,9 @@
#coding:utf8

from sjrpc.core.rpcconnection import *
from sjrpc.core.connectionmanagers import *
from sjrpc.core.callers import *
from sjrpc.core.exceptions import *
from sjrpc.core.async import *

__all__ = ('ConnectionManager', 'RpcConnection', 'RpcCaller', 'RpcError', 
           'ThreadedRpcCaller', )
__all__ = ('RpcConnection', 'RpcCaller', 'ThreadedRpcCaller', 'RpcError',
           'AsyncWatcher')
+27 −12
Changes for sjrpc/core/callers.py: 27 added lines, 12 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/env python
#coding:utf8

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
@@ -11,37 +15,47 @@ class RpcCaller(object):
    peer through it :class:`RpcConnection` object.
    '''

    def __init__(self, request, connection, func):
    def __init__(self, request, protocol, func):
        self._request = request
        self._connection = connection
        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 not getattr(self._func, '__pure__', False):
            args.insert(0, self._connection)

        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:
            self._connection.error(msg_id, message=str(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:
            self._connection.response(msg_id, returned=returned)
            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()


@@ -54,6 +68,7 @@ class ThreadedRpcCaller(RpcCaller):
    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):
Loading