DeferredList中的断言错误

时间:2018-07-30 15:38:46

标签: python twisted

我正在编写一个对时间敏感的小型应用程序,该应用程序应并行执行2个任务:

@inlineCallbacks
def mobile_sipcode(self,number,sessionID): 
    '''This method dips into REST api page'''
    taskStartTime = time.time()

    is_mobile = False 

    payload = {'login':self.dip_username,'pass':self.dip_pass,'tn':number}

    try: 
        resp = yield requests.post(self.dip_url,data=payload,timeout=15)        
    except Exception as err: 
        log.msg ('%s MOBILE_CHECK post has failed with error: %s'%(sessionID,err))


    if DEBUG>25:log.msg('%s MOBILE_CHECK finished for %s Mobile:%s task took: %s'%(sessionID, number, is_mobile,time.time()-taskStartTime))         

    if is_mobile:
        returnValue ('501')
    else:
        returnValue ('200') 



@inlineCallbacks
def dnc_sipcode(self,number,sessionID): 
    '''This method dips into REDIS'''
    taskStartTime = time.time()

    is_dnc = yield self.rdspool.sismember(self.dncRedisKey,number)

    if DEBUG>25:log.msg('%s DNC_CHECK finshed returning %s task took: %s'%(sessionID, is_dnc,time.time()-taskStartTime)) 

    if is_dnc:
        returnValue ('410')
    else:
        returnValue ('200')



@inlineCallbacks
def main ():
    #list of callbacks 
    callbacks_refs = []

    a = threads.deferToThread(dnc_sipcode,dest_num,sessionID)
    b = threads.deferToThread(mobile_sipcode,dest_num,sessionID)

    callbacks_refs.append(a)
    callbacks_refs.append(b)

    try:
        results = yield defer.DeferredList(callbacks_refs)
    except Exception as err:
        log.msg('%s RENDER_RESPONSE results: %s  FAILED WITH ERROR %s '%(sessionID, dest_num,err))

    for result in results:
        log.msg('%s RENDER_RESPONSE phone: %s results from callback list is %s '%(sessionID, dest_num,result))   

当触发时,这两种方法中的代码都可以正确执行,正如我看到的log.msg输出,但是每种方法仍会引发以下异常:

    [-] Unhandled Error
    Traceback (most recent call last):
    File "~lib/python2.7/site-packages/twisted/application/app.py", line 399, in startReactor
    self.config, oldstdout, oldstderr, self.profiler, reactor)
    File "~lib/python2.7/site-packages/twisted/application/app.py", line 312, in runReactorWithLogging
    reactor.run()
    File "~lib/python2.7/site-packages/twisted/internet/base.py", line 1261, in run
    self.mainLoop()
    File "~lib/python2.7/site-packages/twisted/internet/base.py", line 1270, in mainLoop
    self.runUntilCurrent()
    --- <exception caught here> ---
    File "~lib/python2.7/site-packages/twisted/internet/base.py", line 869, in runUntilCurrent
    f(*a, **kw)
    File "~lib/python2.7/site-packages/twisted/internet/defer.py", line 458, in callback
    assert not isinstance(result, Deferred)
    exceptions.AssertionError:

停止执行过去的代码 结果= yield defer.DeferredList(callbacks_refs)块。

异常类似于电子邮件列表中所述: https://twistedmatrix.com/pipermail/twisted-python/2007-October/016128.html

我不能全神贯注于如何使它工作。

任何建议将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

您错误地在线程中使用了非线程安全的Twisted API。在这种情况下,它们具有某些未定义的行为。

如果将函数传递给deferToThread(作为第一近似值),则在该函数的实现中不应使用任何Twisted API。