Asynchronous Bidirectional RPC

房东的猫 提交于 2019-12-03 07:54:31

问题


I am looking for a RPC library in Java or Python (Python is preferred) that uses TCP. It should support:

  • Asynchronous
  • Bidirectional
  • RPC
  • Some sort of event loop (with callbacks or similar)

Any recommendations? I have looked a things like bjsonrpc which seemed to be the right sort of thing however it didn't seem possible for the server to identify which connections; so if a user has identified himself/herself and a request comes in from another user to send a message to that user it doesn't expose that users connection so we can send the message.


回答1:


You should definitely check out Twisted. It's an event-based Python networking framework that has an implementation of an event loop (called the "reactor") supporting select, poll, epoll, kqueue and I/O completion ports, and mediates asynchronous calls with objects called Deferreds

As for your RPC requirement, perhaps you should check out Twisted's PB library or AMP.




回答2:


I'm not entirely sure what you meanאt by "Event loop", but you should check out RPyC (Python)

RPyC Project page




回答3:


i'm the author of bjsonrpc. I'm sure it's possible to do what you want with it.

Some things maybe are poorly documented or maybe some examples are needed.

But, in short, Handlers can store internal states (like authenticated or not, or maybe username). From any handler you can access the "Connection" class, which has the socket itself.

Seems you want something like a chat as an example. I did something similar in the past. I'll try to add a chat example for a new release.

Internal states are explained here: http://packages.python.org/bjsonrpc/tutorial1/index.html#stateful-server

They should be used for authentication (but no standard auth method is provided yet).

On how to reach the connection class from the handler, that isn't documented yet (sorry), but it is used sometimes in the examples inside the source code. For example, example1-server.py contains this public function:

def gettotal(self):
    self._conn.notify.notify("total")
    return self.value_total

BaseHandler._conn represents the connection for that user. And is exactly the same class you get when you connect:

conn = bjsonrpc.connect(host=host,port=port,handler_factory=MyHandler)

So, you can store the connections for logged users in a global variable, and later call any client method you want to.




回答4:


I am involved in developing Versile Python (VPy) which provides the capabilities you are requesting. It is currently available as development releases intended primarily for testing, however you may want to check it out.

Regarding identifying users you can configure remote methods to receive a context object which enables the method to receive information about an authenticated user, using a syntax similar to this draft code.

from versile.quick import *

@doc
class MessageBox(VExternal):
    """Dispatches IM messages."""
    @publish(show=True, doc=True, ctx=True)
    def send_message(self, msg, ctx=None):
        """Sends a message to the message box"""
        if ctx.identity is None:
            raise VException('No authenticated user')
        else:
            # do something ...
            pass


来源:https://stackoverflow.com/questions/7293011/asynchronous-bidirectional-rpc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!