Python 延迟列表中的断言错误
我正在编写一个时间敏感的小应用程序,它应该并行执行两个任务:Python 延迟列表中的断言错误,python,twisted,Python,Twisted,我正在编写一个时间敏感的小应用程序,它应该并行执行两个任务: @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.di
@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:
[-]未处理的错误
回溯(最近一次呼叫最后一次):
文件“~lib/python2.7/site packages/twisted/application/app.py”,第399行,在StartRector中
self.config、oldsdout、oldsderr、self.profiler、reactor)
文件“~lib/python2.7/site packages/twisted/application/app.py”,第312行,位于runReactorWithLogging中
反应堆运行()
文件“~lib/python2.7/site packages/twisted/internet/base.py”,第1261行,正在运行
self.mainLoop()
文件“~lib/python2.7/site packages/twisted/internet/base.py”,第1270行,在mainLoop中
self.rununtlcurrent()
--- ---
文件“~lib/python2.7/site packages/twisted/internet/base.py”,第869行,在rununtlcurrent中
f(*a,**千瓦)
回调中第458行的文件“~lib/python2.7/site packages/twisted/internet/defer.py”
断言不存在(结果,延迟)
异常。断言错误:
它会停止代码的执行
结果=产生延迟。延迟列表(回调\参考)块
例外情况如电子邮件列表中所述:
我不知道如何使它工作
如有任何建议,将不胜感激
感谢您在线程中错误地使用了非线程安全的TwistedAPI。在这种情况下,它们有一些未定义的行为
如果将函数传递给
deletothread
,作为第一种近似方法,则在实现该函数时不应使用任何扭曲的API。感谢您提供的指针。最后我使用了maybeDeferred:a=defer.maybeDeferred(dnc.dnc_sipcode,dest_num,sessionID)b=defer.maybeDeferred(mobile.mobile_sipcode,dest_num,sessionID)