使用autobahn wamp(扭曲)创建独立RPC服务器

2024-10-03 02:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个独立的类,它使用wamp和autobahn提供一个在自己的Python进程中运行的RPC服务器。在

按照这里找到的一些指南,我设法在保存到文件rpcwampserver.py

from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
import multiprocessing
from twisted.internet import reactor

class Manager(ApplicationSession):

    @inlineCallbacks
    def onJoin(self, details):
        print("session ready")

        def test():
            return u'hello!'

    try:
            yield self.register(test, u'rpc.test')
            print("procedure registered")
        except Exception as e:
            print("could not register procedure: {0}".format(e))

class RPCWampServer:
    def __init__(self):
        self._url = u'ws://localhost:8080/ws'
        self.real = u'realm'
        self.runner = ApplicationRunner(url=self._url, realm=self.realm,
                                        #debug=True, debug_wamp=True, debug_app=True
                                        )

    def start(self):
        self.runner.run(Manager, start_reactor=False)

class RPC_Wamp_Server:    
    def __init__(self):
        server = RPCWampServer()
        server.start()
        multi = multiprocessing.Process(target=reactor.run,args=())
        multi.start()

如果RPC_Wamp_服务器直接导入到rpcwampserver.py好的工作守则:

^{2}$

但是

如果我在位于不同路径的包中使用该类,则reactor不工作:

from mymodules.wamp.rpcwampserver import RPC_Wamp_Server
c=RPC_Wamp_Server()

找到类RPC\u Wamp_服务器,但管理器的初始化似乎被跳过。在

启用调试(在代码中注释)时显示:

Starting factory [...]

过了一会儿

Stopped factory [...]

编辑: 使用127.0.0.1而不是localhost解决了这个问题


Tags: fromtestimportself服务器deftwistedrpc