DeferredList 中的断言错误

Assertion Error in DeferredList

我正在编写一个小型的时间敏感应用程序,它应该并行执行 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

我不知道如何让它工作..

任何建议将不胜感激..

谢谢

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

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