最近在学twisted,它的event loop嵌套很深,有什么方法查看回调函数是怎样被调用的吗?
可以在回调函数里面调用traceback.print_stack(),如下图所示,而且也不会中断代码的运行:
import traceback
def stack():
print 'The python stack:'
traceback.print_stack()
from twisted.internet import reactor
reactor.callWhenRunning(stack)
reactor.run()
输出结果如下(most recent call last):
The python stack:
File "/Users/thomas_young/Documents/git_download/introduction-python/twisted-intro-master/basic-twisted/stack.py", line 10, in <module>
reactor.run()
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1266, in run
self.startRunning(installSignalHandlers=installSignalHandlers)
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1246, in startRunning
ReactorBase.startRunning(self)
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 759, in startRunning
self.fireSystemEvent('startup')
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 710, in fireSystemEvent
event.fireEvent()
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 434, in fireEvent
DeferredList(beforeResults).addCallback(self._continueFiring)
File "/Library/Python/2.7/site-packages/twisted/internet/defer.py", line 322, in addCallback
callbackKeywords=kw)
File "/Library/Python/2.7/site-packages/twisted/internet/defer.py", line 311, in addCallbacks
self._runCallbacks()
File "/Library/Python/2.7/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 447, in _continueFiring
callable(*args, **kwargs)
File "/Users/thomas_young/Documents/git_download/introduction-python/twisted-intro-master/basic-twisted/stack.py", line 6, in stack
traceback.print_stack()
网友评论