Skip to main content
 首页 » 编程设计

python之WSDL 使用 SUDS 加载缓慢

2024年11月01日28java哥

我们正在与 Vindicia 一起实现新的计费系统。 Vindicia 有一个很棒的 wsdl 文件,可以很容易地创建一个模块。所以我们是 SUDS。但问题是 SUDS 在加载那些 wsdl 时真的很慢。 (在我们的例子中,它最多需要 2.4 秒)。

以下是我如何使用 SUDS 实现它。

class BaseWSDL(object): 
    client = None 
    group = "" 
    instances = "" 
 
    @classmethod 
    def get_client(cls): 
        if cls.client is None: 
            wsdl = 'file://%s/%s.wsdl' % (wsdl_dir, cls.group) 
 
            cls.client = Client(url=wsdl, location=host) 
            setattr(cls, cls.instances.split(":")[1].lower(), cls.client.factory.create(cls.instances)) 
        return cls.client 
 
class Authentication(object): 
    def __init__(self, client, instances): 
        self.authentication = client.factory.create(instances) 
        self.authentication.login = login 
        self.authentication.password = pw 
 
class BillingPlan(BaseWSDL): 
    group = "BillingPlan" 
    instances = "ns2:BillingPlan" 
 
    def __init__(self, **kwargs): 
        super(BillingPlan, self).__init__() 
 
    def fetch_all(self): 
        client = self.get_client() 
        auth = Authentication(client, "ns2:Authentication") 
        response = client.service.fetchAll(auth.authentication) 
        if response[0].returnCode == "200": 
            plans_dict = {} 
            for plan in response[1]: 
                plans_dict[plan.merchantBillingPlanId] = plan 
            return plans_dict 

但这里的问题是cls.client = Client(url=wsdl, location=settings.VIN_SOAP_HOST) 第一次看 2 次。但是我们为新请求重用同一个对象,我们担心 SUDS 不是线程最安全的。

所以我们寻找另一个简单的解决方案。而且我们发现 pySimpleSoap 快得多。

但我们在加载 wsdl 期间遇到递归错误。 (哪个接缝是一个已知问题,代码中有一个 TODO 讨论递归)

... 
    File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/helpers.py", line 205, in postprocess_element 
    postprocess_element(n) 
  File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/helpers.py", line 188, in postprocess_element 
    postprocess_element(v) 
  File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/helpers.py", line 188, in postprocess_element 
    postprocess_element(v) 
  File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/helpers.py", line 205, in postprocess_element 
    postprocess_element(n) 
  File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/helpers.py", line 188, in postprocess_element 
    postprocess_element(v) 
  File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/helpers.py", line 185, in postprocess_element 
    for k, v in elements.items(): 
  File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/simplexml.py", line 151, in items 
    return [(key, self[key]) for key in self.__keys] 
RuntimeError: maximum recursion depth exceeded while calling a Python object</code> 

因此,我们正在寻找一种可以降低 Wsdl 负载的解决方案。您会建议在创建客户端后对其进行缓存吗?然后重用它? 而且它必须易于实现。我们希望我们不必重新实现所有功能。

请您参考如下方法: