码迷,mamicode.com
首页 > 其他好文 > 详细

记 suds 模块循环依赖的坑

时间:2016-04-18 18:32:19      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

下面是soa接口调用的核心代码

#! /usr/bin/python
# coding:utf-8
from suds.client import Client
def SoaRequest(wsdl,fnname,data): soaService = Client(wsdl).service soaRep = getattr(soaService,fnname)(data) return soaRep

问题就这样出现了:

我调用一个接口,总是报错,见下图:

技术分享

之后Debug断点定位到suds模块的sxbasic.py文件中的Import类的open方法

    def open(self, options):"""
        Open and import the refrenced schema.
        @param options: An options dictionary.
        @type options: L{options.Options}
        @return: The referenced schema.
        @rtype: L{Schema}
        """
        if self.opened:
            return
        self.opened = True
        log.debug(%s, importing ns="%s", location="%s", self.id, self.ns[1], self.location)
        result = self.locate()

        if result is None:
            if self.location is None:
                log.debug(imported schema (%s) not-found, self.ns[1])
            else:
                result = self.download(options)
        log.debug(imported:\n%s, result)
        return result
self.location 为None时日志打印,否则就执行download
而我测试的接口有个这种问题,A中依赖B,B中依赖C,C中依赖A,循环依赖
所有就出问题了

好吧,那就改吧
在类Import外定义数组变量resultList = []
    def open(self, options):
        global resultList#声明全局变量
        if self.opened:
            return
        self.opened = True
        log.debug(%s, importing ns="%s", location="%s", self.id, self.ns[1], self.location)
        result = self.locate()

        if result is None:
            if self.location is None:
                log.debug(imported schema (%s) not-found, self.ns[1])
            else:
                if self.location in resultList:
                    log.debug(location is already in resultList) #list中存在,即说明已完成下载
                else:
                    result = self.download(options)
                    resultList.append(self.location)#下载完成后,往list中添加记录
        log.debug(imported:\n%s, result)
        return result

这样应该没问题了吧,运行,还是报同样的错

继续调试

发现resultList一直是空,就是说下载操作完成后添加记录的操作没有执行

于是我调换了下顺序

直接成功

 

   def open(self, options):
        global resultList
        if self.opened:
            return
        self.opened = True
        log.debug(%s, importing ns="%s", location="%s", self.id, self.ns[1], self.location)
        result = self.locate()

        if result is None:
            if self.location is None:
                log.debug(imported schema (%s) not-found, self.ns[1])
            else:
                if self.location in resultList:
                    log.debug(location is already in resultList)
                else:
                    resultList.append(self.location)
                    result = self.download(options)
        log.debug(imported:\n%s, result)
        return result

 

记 suds 模块循环依赖的坑

标签:

原文地址:http://www.cnblogs.com/hito/p/5405226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!