Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让twisted打印未处理错误的确切位置_Python_Twisted - Fatal编程技术网

Python 如何让twisted打印未处理错误的确切位置

Python 如何让twisted打印未处理错误的确切位置,python,twisted,Python,Twisted,我正在写一个简单的服务器程序。它们将是新代码中不可避免的打字错误和其他错误,通常python解释器将打印ValueError/AttributeError回溯并退出。回溯可以指向错误的确切位置。但是在twisted框架下,这些错误不会被打印出来。如以下示例所示: from twisted.internet import reactor, protocol, task #from twisted.internet.defer import setDebugging #setDebugging(Tr

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

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()
MyFactory.annound中的
A_GREAT_TYPO_HERE()
应该是
get_position()
。但这是一个打字错误

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

延迟中未处理的错误:

没有别的了。即使启用延迟调试(取消第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)
)
延迟中未处理的错误: (调试:C:已创建延迟: C:文件“nodes/test.py”,第48行,在 C:main() C:文件“nodes/test.py”,第43行,在main中 C:myfactory=myfactory() C:文件“nodes/test.py”,第21行,在_init中__ C:自我信用证启动(1) C:文件“/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/task.py”,第189行,开头 C:deferred=self.\u deferred=deferred.deferred() I:第一个调用程序是: I:文件“nodes/test.py”,第48行,在 一:主要内容() I:文件“nodes/test.py”,第43行,在main中 I:myfactory=myfactory() I:文件“nodes/test.py”,第21行,在_init中__ I:self.lc.start(1) I:文件“/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/task.py”,第194行,开头 I:self() I:File“/home/sgsdxzy/anaconda3/lib/python3.6/site packages/twisted/internet/task.py”,第241行,在调用中__ I:d.加法器(eb) I:文件“/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/defer.py”,第332行,在补遗中 I:errbackKeywords=kw) I:addCallbacks中的文件“/home/sgsdxzy/anaconda3/lib/python3.6/site packages/twisted/internet/defer.py”,第310行 I:self.\u runCallbacks() I:File“/home/sgsdxzy/anaconda3/lib/python3.6/site-packages/twisted/internet/defer.py”,第653行,运行回调 I:current.result=回调(current.result,*args,**kw) I:文件“/home/sgsdxzy/anaconda3/lib/python3.6/site packages/twisted/internet/task.py”,第236行,电子商务 I:d.errback(失败) ) 它将错误指向与
self.lc.start(1)
,而不是
A\u GREAT\u TYPO\u HERE()
。如何调试程序,以便回溯可以指向实际错误?

您看到的“C”和“I”行是由于启用了延迟调试。“C”行提供了创建延迟的堆栈。“I”行为您提供了“调用延迟”的堆栈(调用了它的
callback
errback
方法)

这两个似乎都不是你想要的。如果您想查看与触发延迟的
故障
相关联的堆栈,最简单的解决方案是确保记录
故障
(并且您有一个日志观察程序,以便您可以实际查看该日志事件)

您应该将其添加到您的
main

from sys import stdout
from twisted.logger import globalLogBeginner, textFileLogObserver
globalLogBeginner.beginLoggingTo([textFileLogObserver(stdout)])
这会将日志流作为文本定向到标准输出。这很可能足以让你得到你想要的信息。然而,为了真正的安全,您还希望显式地记录故障,而不是依赖垃圾收集器来为您做这件事。因此,您还需要更改:

self.lc.start(1)
致:

(您也可以考虑将该代码从Sy-IntIs中取出,并将其放入StestPrime中;也不要考虑使用全局反应器,而是将其作为参数传递)。 这将为您提供如下输出:

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
2017-04-25T06:53:14-0400
回溯(最近一次呼叫最后一次):
文件“debugging2.py”,第52行,在main中
myfactory=myfactory()
文件“debugging2.py”,第28行,在_init中__
d=自lc启动(1)
文件“/tmp/debuging/local/lib/python2.7/site packages/twisted/internet/task.py”,第194行,开始
self()
文件“/tmp/debug/local/lib/python2.7/site packages/twisted/internet/task.py”,第239行,在调用中__
d=延迟。可能会出错(self.f、*self.a、**self.kw)
---  ---
文件“/tmp/debug/local/lib/python2.7/site packages/twisted/internet/defer.py”,第150行,格式为maybeDeferred
结果=f(*参数,**kw)
文件“debugging2.py”,第32行,在公告中
pos=A_GREAT_TYPO_HERE()
exceptions.NameError:未定义全局名称“A_GREAT_TYPO_HERE”

您使用的Twisted版本是什么?@Jean-Paul Calderone它是17.1.0,python版本是3.6.1Thanks!记录器提供了很多有用的信息,我将阅读关于它可以提供什么的文档。
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