我正在学习在Twisted上编写应用程序,因此会犯很多错误。
假设有一个代码:
d = defer.Deferred()
d.addCallback(self.start_app)
#d.addErrback(self.command_die)
d.callback(0)
def start_app(self, d):
#import os
return os.startfile(self.path)
def command_die(self, d):
print ('com_die', d)
我们在os.startfile(self.path)和Big Traceback中有错误:
Unhandled error in Deferred:
Unhandled Error
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 542, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:/Dropbox/my_py/client3.py", line 100, in command_analiz
d.callback(i)
File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 361, in callback
self._startRunCallbacks(result)
File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 455, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 542, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:/Dropbox/my_py/client3.py", line 353, in start_eve_d
return os.startfile(self.path)
exceptions.NameError: global name 'os' is not defined
如果我取消注释#d.addErrback(self.command_die)
我们通过print('com_die',d)创建了很少的“log”:
('com_die', <twisted.python.failure.Failure <type 'exceptions.NameError'>>)
有没有办法在设计时获得完整的Log \ TraceBack?
目前我要了解哪里出现了新错误,有必要对addErrorback进行评论
答案 0 :(得分:5)
Twisted Failure对象拥有您需要的一切。 要获得良好的回溯,请尝试使用 printTraceback()方法:
>>> from twisted.internet.defer import Deferred as D
>>> def start_app(_):
... #import os
... return os.startfile('sasa')
...
... def command_die(err):
... err.printTraceback()
...
...
... d = D()
... d.addCallback(start_app)
... d.addErrback(command_die)
... d.callback(0)
Traceback (most recent call last):
File "C:\Users\Pilyavskiy\AppData\Local\DreamPie\share\dreampie\subp-py2\dreampielib\subprocess\__init__.py", line 324, in execute
exec codeob in self.locs
File "<pyshell#3>", line 12, in <module>
d.callback(0)
File "C:\pill\Python27\lib\site-packages\twisted\internet\defer.py", line 361, in callback
self._startRunCallbacks(result)
File "C:\pill\Python27\lib\site-packages\twisted\internet\defer.py", line 455, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "C:\pill\Python27\lib\site-packages\twisted\internet\defer.py", line 542, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "<pyshell#3>", line 3, in start_app
return os.startfile('sasa')
exceptions.NameError: global name 'os' is not defined
在源代码http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/python/failure.py#L121中查找其他选项,它的评论非常好。
PS:名称 d 主要用于延迟本身,对于结果/错误,最好使用 res / err 等< / p>