使用Tornado和SUDS进行异步SOAP调用

时间:2014-02-27 21:19:40

标签: python soap asynchronous tornado suds

我正在与Tornado和SUDS合作。我想使用Tornados AsyncHTTPClient进行异步调用。这是我的代码

class RequestHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
    data = tornado.escape.json_decode(self.request.body)
    uuid = data['id']
    suds_client = Client(wsdl_location, nosend=True) 
    context = suds_client.service.GetSubscriptions(uuid)        
    tornado_client = tornado.httpclient.AsyncHTTPClient()
    tornado_client.fetch(context.envelope,callback=self.on_response)

def on_response(self,response):
    print response
    #self.write(str(response))
    self.finish()

我根据帖子here

中的讨论在上面的代码中设置了nosend = True

它说要设置nosend = True并构建一个信封然后使用Tornado异步http代理来获取它。

当我执行上面的代码时,我没有得到任何回复。 我该怎么做?任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:3)

我自己解决了。我将在下面提供我的代码以供参考和理解。

class RequestHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
    data = tornado.escape.json_decode(self.request.body)
    uuid = data['id']
    suds_client = Client(wsdl_location, transport = trans_certs, nosend=True)
    context = suds_client.service.GetSubscriptions(uuid)
    tornado_client = tornado.httpclient.AsyncHTTPClient()
    url=context.client.location()
    tornado_client.fetch(url,body=str(context.envelope),method="POST",headers=context.client.headers(),callback=self.on_response)


def on_response(self,response):
    result=str(response.body)
    dom1 = parseString(result)
    for node in dom1.getElementsByTagName('subscriptionId'):
        self.write(str(node.toxml()))
    self.finish()

答案 1 :(得分:0)

感谢。它工作得很好。为了在Tornado中进行异步SOAP调用,我们也可以使用ThreadPoolExecutor类,它在其他线程中而不是在ioloop中执行阻塞方法。



    from concurrent.futures import ThreadPoolExecutor
    import tornado.ioloop
    from tornado import concurrent
    from tornado import gen

    class asyncExec(object):

      def __init__(self,ioloop = None):
        self.executor = ThreadPoolExecutor(max_workers=10)
        self.io_loop = ioloop or tornado.ioloop.IOLoop.instance()

      #client is the suds client and remoteMethod is the blocking function

      @run_on_executor
      def getResponse(self,client,args):
        response = client.service.remoteMethod(args)
        return(response)


    #getResponse returns a future object which can be yielded in a coroutine :

  @gen.coroutine
  def makeRequest():
    asyncCaller = AsyncExec()
    response = yield asyncCaller.getResponse(client,args)
    #Do something with response