我将Autobahn PRC客户端功能定义如下:
def call_server_get_voltage(self):
self.call("http://server.com/server#get_voltage").addCallback(self.on_get_voltage)
def on_get_voltage(self, result_voltage):
print "on_get_voltage: " + result_voltage
上面的代码调用RPC服务器函数" get_voltage"。它工作正常,从RPC服务器正确读取电压。
但是,我想进行同步通话。它应该如下所示,而不是在回调函数中获得值。
def call_server_synchronous_get_voltage(self):
(How to combine above callback function, so that it will be synchronous)
return voltage
我试过了:
a)inlinecallback装饰器。但是,它仍然返回延迟对象。
@inlineCallbacks
def call_server_synchronous_get_voltage_inlinecallback(self):
#'val' is correct value from server
val = yield self.self.call("http://server.com/server#get_voltage")
#but how to return it????
returnValue(val) #This is deferred object. Not voltage value
有人可以帮我理解@inlineCallback吗?这是为此目的的装饰者吗?
b)我试过Crochet @wait_for装饰。它应该使回调从调用者中消失。
@wait_for(timeout=10)
def call_server_synchronous_get_voltage_crochet(self):
val = self.self.call("http://server.com/server#get_voltage")
val.addCallback(lambda result: result)
return val
此函数确实返回正确的值。但它只工作一次。我从Django服务器调用call_server_synchronous_get_voltage_crochet()
来显示网页。当第二次重新加载网页时,将丢弃与服务器的高速公路RPC连接,并显示错误:
Lost client connection: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]
如何调用Autobahn RPC并同步获取结果?
答案 0 :(得分:0)
要使用Crochet,你可能不应该屈服于任何东西。
@wait_for(timeout=10)
def call_server_synchronous_get_voltage_crochet(self):
val = self.self.call("http://server.com/server#get_voltage")
val.addCallback(lambda result: result)
return val