如何让扭曲打印未处理错误的确切位置

How to let twisted print the exact positions of unhandled errors

我正在编写一个简单的服务器程序。它们将是新代码中不可避免的拼写错误和其他错误,通常 python 解释器将打印 ValueError/AttributeError 回溯并退出。回溯可以指向错误的确切位置。但是在扭曲的框架下,这些错误不会被打印出来。就像下面的例子:

from twisted.internet import reactor, protocol, task
#from twisted.internet.defer import setDebugging
#setDebugging(True)

class MyProtocol(protocol.Protocol):
    def dataReceived(self, data):
        try:
            set_position(int(data))
        except ValueError:
            pass
    def connectionMade(self):
        self.factory.clientConnectionMade(self)
    def connectionLost(self, reason):
        self.factory.clientConnectionLost(self)

class MyFactory(protocol.Factory):
    protocol = MyProtocol
    def __init__(self):
        self.clients = []
        self.lc = task.LoopingCall(self.announce)
        self.lc.start(1)

    def announce(self):
        pos = A_GREAT_TYPO_HERE()
        for client in self.clients:
            client.transport.write("Posiiton is {0}\n".format(pos).encode('utf-8'))

    def clientConnectionMade(self, client):
        self.clients.append(client)

    def clientConnectionLost(self, client):
        self.clients.remove(client)

def get_position():
    return position[0]

def set_position(pos):
    position[0] = pos

def main():
    global position
    position = [0]
    myfactory = MyFactory()
    reactor.listenTCP(5362, myfactory)
    reactor.run()

if __name__ == "__main__":
    main()

A_GREAT_TYPO_HERE() 在 MyFactory.announce 中的意思是 get_position()。但打错了。

而服务器为运行时,终端只输出

Unhandled error in Deferred:

仅此而已。即使我启用延迟调试(取消注释第 2 行和第 3 行),终端输出:

Unhandled error in Deferred:
(debug:  C: Deferred was created:
 C:  File "nodes/test.py", line 48, in <module>
 C:    main()
 C:  File "nodes/test.py", line 43, in main
 C:    myfactory = MyFactory()
 C:  File "nodes/test.py", line 21, in __init__
 C:    self.lc.start(1)
 C:  File "/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/task.py", line 189, in start
 C:    deferred = self._deferred = defer.Deferred()
 I: First Invoker was:
 I:  File "nodes/test.py", line 48, in <module>
 I:    main()
 I:  File "nodes/test.py", line 43, in main
 I:    myfactory = MyFactory()
 I:  File "nodes/test.py", line 21, in __init__
 I:    self.lc.start(1)
 I:  File "/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/task.py", line 194, in start
 I:    self()
 I:  File "/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/task.py", line 241, in __call__
 I:    d.addErrback(eb)
 I:  File "/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/defer.py", line 332, in addErrback
 I:    errbackKeywords=kw)
 I:  File "/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/defer.py", line 310, in addCallbacks
 I:    self._runCallbacks()
 I:  File "/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
 I:    current.result = callback(current.result, *args, **kw)
 I:  File "/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/task.py", line 236, in eb
 I:    d.errback(failure)
)

它指出错误接近 self.lc.start(1),但不是 A_GREAT_TYPO_HERE()。我如何调试我的程序以便回溯可以指向实际错误?

您看到的 "C" 和 "I" 行是因为您启用了延迟调试。 "C" 行为您提供了创建 Deferred 的堆栈。 "I" 行为您提供 Deferred 所在的堆栈 "invoked"(它的 callbackerrback 方法被调用)。

看来,这些都不是您要找的。如果您想查看与 Failure 关联的堆栈,Deferred 已被触发,最直接的解决方案是确保 Failure 被记录(并且您有一个日志观察器,以便您可以实际上看到那个日志事件)。

您应该将此添加到您的 main:

from sys import stdout
from twisted.logger import globalLogBeginner, textFileLogObserver
globalLogBeginner.beginLoggingTo([textFileLogObserver(stdout)])

这会将日志流作为文本定向到标准输出。它很可能足以让您获得所需的信息。然而,为了真正安全,您还希望显式记录失败,而不是依赖垃圾收集器为您做这件事。所以你也想改:

self.lc.start(1)

收件人:

# Module scope
from twisted.logger import Logger
logger = Logger()

...

# in __init__
d = self.lc.start(1)
d.addErrback(lambda f: logger.failure("Loop thing problem", f))

(您可能还需要考虑将此代码从 __init__ 中取出并放入 startFactory 中;还可以考虑不使用全局反应器,而是将其作为参数传递。)

这将为您提供如下输出:

2017-04-25T06:53:14-0400 [__main__.MyFactory#critical] Foo
        Traceback (most recent call last):
          File "debugging2.py", line 52, in main
            myfactory = MyFactory()
          File "debugging2.py", line 28, in __init__
            d = self.lc.start(1)
          File "/tmp/debugging/local/lib/python2.7/site-packages/twisted/internet/task.py", line 194, in start
            self()
          File "/tmp/debugging/local/lib/python2.7/site-packages/twisted/internet/task.py", line 239, in __call__
            d = defer.maybeDeferred(self.f, *self.a, **self.kw)
        --- <exception caught here> ---
          File "/tmp/debugging/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 150, in maybeDeferred
            result = f(*args, **kw)
          File "debugging2.py", line 32, in announce
            pos = A_GREAT_TYPO_HERE()
        exceptions.NameError: global name 'A_GREAT_TYPO_HERE' is not defined