python sudsライブラリを使用して、ローカルの wsdl ファイルに基づいて SOAP クライアントを作成しています。私の目標は Twisted をバックエンドとして使用することなので、SOAP サーバーに非同期でクエリを実行します。
このトピックが何度も取り上げられてきたことは知っていますが ( here1、here2 )、まだいくつか質問があります。
私は泡でツイストを使用する 3 つの異なるアプローチを見てきました。
a) このパッチを suds ライブラリに適用します。
b)泡のフォークであるtwisted-sudsを使用します。
c)この投稿Client_Async
の影響を受けて、ツイスト操作を使用して suds クライアントを実装しましたdeferToThread
(完全に動作する要点はここClient_Sync
にあります。いくつかのベンチマークを実行するために suds クライアントも実装しました)。
# 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
私はアプローチ a) または b) をテストしていません。私の質問は次のとおりです。
スレッドを 1 つだけ使用する以外に、それらから実際に得られるものは何ですか?