2013-10-24 78 views
4

我使用python suds庫作爲基於本地wsdl文件的SOAP客戶端。我的目標是使用Twisted作爲後端,所以我以異步的方式查詢SOAP服務器。扭曲泡沫的好處 - 異步使用蟒蛇肥皂的方式lib

我知道這個話題已經涵蓋了不同的時間(here1, here2),但我仍然有一些問題。


我已經看到了三種不同的方法來使用雙絞線用肥皂水:

一)運用這一patch到泡沫庫。

b)使用twisted-suds,這是泡沫分叉。

c)按this post影響,我使用扭曲deferToThread操作來實現Client_Async泡沫客戶端,(完全工作主旨可以發現here。我還實施了Client_Sync泡沫客戶端,以及做一些基準)

# Init approach c) code 
from suds.client import Client as SudsClient 
from twisted.internet.threads import deferToThread 

class MyClient(SudsClient): 
    def handleFailure(self, f, key, stats): 
     stats.stop_stamp(error=True) 
     logging.error("%s. Failure: %s" % (key, str(f))) 

    def handleResult(self, result, key, stats): 
     stats.stop_stamp(error=False) 
     success, text, res = False, None, None 
     try: 
      success = result.MessageResult.MessageResultCode == 200 
      text = result.MessageResult.MessageResultText 
      res = result.FooBar 
     except Exception, err: 
      pass 
     logging.debug('%40s : %5s %10s \"%40s\"' % (key, success, text, res)) 
     logging.debug('%40s : %s' % (key, self.last_sent())) 
     logging.debug('%40s : %s' % (key, self.last_received())) 

def call(stats, method, service, key, *a, **kw): 
    stats.start_stamp() 
    logging.debug('%40s : calling!' % (key)) 
    result = service.__getattr__(method)(*a, **kw) 
    return result 

class Client_Async(MyClient): 
    """ Twisted based async client""" 
    def callRemote(self, stats, method, key, *args, **kwargs): 
     logging.debug('%s. deferring to thread...' % key) 
     d = deferToThread(call, stats, method, self.service, key, *args, **kwargs) 
     d.addCallback(self.handleResult, key, stats) 
     d.addErrback(self.handleFailure, key, stats) 
     return d 

class Client_Sync(MyClient): 
    def callRemote(self, stats, method, key, *args, **kwargs): 
     result = None 
     try: 
      result = call(stats, method, self.service, key, *args, **kwargs) 
     except Exception, err: 
      self.handleFailure(err, key, stats) 
     else: 
      self.handleResult(result, key, stats) 
# End approach c) code 

用c)的方式分異步模式的好處做一個小的基準:

-- Sync model using Client_Sync of approach c). 
# python soap_suds_client.py -t 200 --sync 
Total requests:800/800. Success:794 Errors:6 
Seconds elapsed:482.0 
Threads used:1 

-- Async model using Client_Async of approach c). 
# python soap_suds_client.py -t 200 
Total requests:800/800. Success:790 Errors:10 
Seconds elapsed:53.0 
Threads used:11 

我沒有測試接近)或b),我的問題是:

什麼我真的從他們身上獲得除了只使用一個線程?

回答

5

我在我的項目中使用了泡沫。我沒有做任何補丁,或使用扭曲的肥皂。 我使用python-suds軟件包(在Ubuntu上)的0.4.1-2版本,它配備了非常有用的nosend選項。

# This parses the wsdl file. The autoblend option you'd probably skip, 
    # its needed when name spaces are not strictly preserved (case for Echo Sign). 
    from suds import client 
    self._suds = client.Client('file://' + config.wsdl_path, nosend=True, 
           autoblend=True) 

    .... 

    # Create a context for the call, example sendDocument() call. This doesn't yet 
    # send anything, only creates an object with the request and capable of parsing 
    # the response 
    context = self._suds.service.sendDocument(apiKey=....) 

    # Actually send the request. Use any web client you want. I actually use 
    # something more sophisticated, but below I put the example using 
    # standard twisted web client. 
    from twisted.web import client 
    d = client.getPage(url=context.client.location(), 
         postdata=str(context.envelope), 
         method='POST', 
         headers=context.client.headers()) 

    # The callback() of the above Deferred is fired with the body of the 
    # http response. I parse it using the context object.   
    d.addCallback(context.succeeded) 

    # Now in the callback you have the actual python object defined in 
    # your WSDL file. You can print... 
    from pprint import pprint 
    d.addCallback(pprint) 
    # I the response is a failure, your Deferred would be errbacked with 
    # the suds.WebFault exception. 
+0

是否需要v0.4.1-2? 'nosend'選項似乎回到0.4 https://fedorahosted.org/suds/wiki/Documentation#OPTIONS –

+0

我不得不承認,我還沒有測試任何其他版本。我只是想給出一個特定的答案,指出我知道它確實有效的版本。 –

+0

是的,有道理 - 謝謝澄清 –