Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when complex objects are passed as argument #25

Open
jpmercier opened this issue Jun 15, 2019 · 0 comments
Open

Issue when complex objects are passed as argument #25

jpmercier opened this issue Jun 15, 2019 · 0 comments

Comments

@jpmercier
Copy link

jpmercier commented Jun 15, 2019

In the Python version, when called using complex Python object with __str__ method the rpc fails as the object converted to its string representation.

The fix is relatively simple:

  1. Modify the FunctionCall object:
    1.1. Change the __init__ function
    1.2. Created a function call call that first get the function attrbute name of the object and then pass the args and kwargs
class FunctionCall(dict):
    """Encapsulates a function call as a Python dictionary."""

    @staticmethod
    def from_dict(dictionary):
        """Return a new FunctionCall from a Python dictionary."""
        name = dictionary.get('name')
        args = dictionary.get('args')
        kwargs = dictionary.get('kwargs')
        return FunctionCall(name, args, kwargs)

    def __init__(self, name, args=None, kwargs=None):
        """Create a new FunctionCall from a method name, an optional argument tuple, and an optional keyword argument
        dictionary."""
        self['name'] = name
        if not args:
            self['args'] = []
        else:
            self['args'] = args
        if not kwargs:
            self['kwargs'] = [{}]
        else:
            self['kwargs'] = kwargs

    def call(self, local_object):
        func = getattr(local_object, self['name'])
        return func(*self['args'], **self['kwargs'])

    def as_python_code(self):
        """Return a string representation of this object that can be evaled to execute the function call."""
        argstring = '' if 'args' not in self else \
                ','.join(str(arg) for arg in self['args'])
        kwargstring = '' if 'kwargs' not in self else \
                ','.join('%s=%s' % (key,val) for (key,val) in list(self['kwargs'].items()))
        if len(argstring) == 0:
            params = kwargstring
        elif len(kwargstring) == 0:
            params = argstring
        else:
            params = ','.join([argstring,kwargstring])
        return '%s(%s)' % (self['name'], params)
  1. The Server class also needs to be modified to replace the exec statement with function_call.call(self.local_object)
class Server(object):
    """Executes function calls received from a Redis queue."""

    def __init__(self, redis_server, message_queue, local_object):
        self.redis_server = redis_server
        self.message_queue = message_queue
        self.local_object = local_object
 
    def run(self):
        # Flush the message queue.
        self.redis_server.delete(self.message_queue)
        while True:
            message_queue, message = self.redis_server.blpop(self.message_queue)
            message_queue = message_queue.decode()
            assert message_queue == self.message_queue
            logging.debug('RPC Request: %s' % message)
            transport, rpc_request = decode_message(message)
            response_queue = rpc_request['response_queue']
            function_call = FunctionCall.from_dict(rpc_request['function_call'])
            self.value = function_call.call(self.local_object)
            rpc_response = dict(return_value=self.value)
            message = transport.dumps(rpc_response)
            logging.debug('RPC Response: %s' % message)
            self.redis_server.rpush(response_queue, message)
@jpmercier jpmercier changed the title Issue when complex object passed as argument Issue when complex objects are passed as argument Jun 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant