gpt4 book ai didi

python - 使用 Tornado 的嵌套 Web 服务调用(异步?)

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:31 24 4
gpt4 key购买 nike

我正在使用 tornado(和第三方 tornadows 模块)实现 SOAP 网络服务。我服务中的一个操作需要调用另一个,所以我有链:

  1. (通过 SOAPUI)对操作 A 的外部请求
  2. 内部请求(通过请求模块)进入操作 B
  3. 操作B的内部响应
  4. 操作A的外部响应

因为它都在一个服务中运行,所以它在某处被阻止了。我不熟悉 Tornado 的异步功能。

只有一种请求处理方法(post),因为所有内容都来自单个 url,然后根据 SOAPAction 请求 header 值调用特定操作(进行处理的方法)。我用 @tornado.web.asynchronous 装饰了我的 post 方法,并在最后调用了 self.finish() 但没有骰子。

tornado 可以处理这种情况吗?如果可以,我该如何实现?

编辑(添加代码):

class SoapHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
""" Method post() to process of requests and responses SOAP messages """
try:
self._request = self._parseSoap(self.request.body)
soapaction = self.request.headers['SOAPAction'].replace('"','')
self.set_header('Content-Type','text/xml')
for operations in dir(self):
operation = getattr(self,operations)
method = ''
if callable(operation) and hasattr(operation,'_is_operation'):
num_methods = self._countOperations()
if hasattr(operation,'_operation') and soapaction.endswith(getattr(operation,'_operation')) and num_methods > 1:
method = getattr(operation,'_operation')
self._response = self._executeOperation(operation,method=method)
break
elif num_methods == 1:
self._response = self._executeOperation(operation,method='')
break
soapmsg = self._response.getSoap().toprettyxml()
self.write(soapmsg)
self.finish()
except Exception as detail:
#traceback.print_exc(file=sys.stdout)
wsdl_nameservice = self.request.uri.replace('/','').replace('?wsdl','').replace('?WSDL','')
fault = soapfault('Error in web service : {fault}'.format(fault=detail), wsdl_nameservice)
self.write(fault.getSoap().toxml())
self.finish()

这是来自请求处理程序的 post 方法。它来 self 正在使用的 Web 服务模块(所以不是我的代码),但我添加了异步装饰器和 self.finish()。它基本上所做的就是调用正确的操作(如请求的 SOAPAction 中所指示)。

class CountryService(soaphandler.SoapHandler):
@webservice(_params=GetCurrencyRequest, _returns=GetCurrencyResponse)
def get_currency(self, input):
result = db_query(input.country, 'currency')
get_currency_response = GetCurrencyResponse()
get_currency_response.currency = result
headers = None
return headers, get_currency_response

@webservice(_params=GetTempRequest, _returns=GetTempResponse)
def get_temp(self, input):
get_temp_response = GetTempResponse()
curr = self.make_curr_request(input.country)
get_temp_response.temp = curr
headers = None
return headers, get_temp_response

def make_curr_request(self, country):

soap_request = """<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:coun='CountryService'>
<soapenv:Header/>
<soapenv:Body>
<coun:GetCurrencyRequestget_currency>
<country>{0}</country>
</coun:GetCurrencyRequestget_currency>
</soapenv:Body>
</soapenv:Envelope>""".format(country)

headers = {'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': '"http://localhost:8080/CountryService/get_currency"'}
r = requests.post('http://localhost:8080/CountryService', data=soap_request, headers=headers)
try:
tree = etree.fromstring(r.content)
currency = tree.xpath('//currency')
message = currency[0].text
except:
message = "Failure"
return message

这是网络服务的两个操作(get_currency 和 get_temp)。所以 SOAPUI 命中 get_temp,它向 get_currency 发出 SOAP 请求(通过 make_curr_request 和请求模块)。然后结果应该只是回链并发送回 SOAPUI。

服务的实际操作毫无意义(在询问温度时返回货币),但我只是想让功能正常工作,这些是我的操作。

最佳答案

我不认为您的 soap 模块或请求是异步的。

我相信添加@asyncronous 装饰器只是成功的一半。现在你没有在你的函数内部发出任何异步请求(每个请求都是阻塞的,这会占用服务器直到你的方法完成)

您可以使用 tornados AsynHttpClient 来切换它.这几乎可以用作请求的确切替代品。来自docoumentation示例:

class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
http = tornado.httpclient.AsyncHTTPClient()
http.fetch("http://friendfeed-api.com/v2/feed/bret",
callback=self.on_response)

def on_response(self, response):
if response.error: raise tornado.web.HTTPError(500)
json = tornado.escape.json_decode(response.body)
self.write("Fetched " + str(len(json["entries"])) + " entries "
"from the FriendFeed API")
self.finish()

他们的方法是用异步装饰的,并且他们正在发出异步 http 请求。这是流程变得有点奇怪的地方。当您使用 AsyncHttpClient 时,它不会锁定事件循环(请注意,我这周才开始使用 tornado,如果我的所有术语都不正确,请放轻松)。这允许服务器自由处理传入的请求。当您的 asynchttp 请求完成时,将执行回调方法,在本例中为 on_response

在这里,您可以轻松地用 tornado asynchttp 客户端替换请求。但是,对于您的肥皂服务,事情可能会更复杂。您可以围绕您的 soap 客户端创建一个本地 webserivce,并使用 tornado asyn http 客户端向它发出异步请求???

这将创建一些复杂的回调逻辑,可以使用 gen decorator 修复这些逻辑

关于python - 使用 Tornado 的嵌套 Web 服务调用(异步?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14586038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com