- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
有没有办法阻止 Twisted reactor 自动吞下异常(例如 NameError)?我只是想让它停止执行,并在控制台中给我一个堆栈跟踪?
甚至还有常见问题解答 question关于它,但至少可以说,它不是很有帮助。
目前,在每个错误返回中,我都这样做:
def errback(value):
import traceback
trace = traceback.format_exc()
# rest of the errback...
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.internet import protocol, reactor
class Broken(protocol.Protocol):
def connectionMade(self):
buggy_user_code()
e = TCP4ClientEndpoint(reactor, "127.0.0.1", 22)
f = protocol.Factory()
f.protocol = Broken
e.connect(f)
reactor.run()
> python2.7 tx-example.py
^CUnhandled error in Deferred:
Unhandled Error
Traceback (most recent call last):
Failure: twisted.internet.error.ConnectionRefusedError: Connection was refused by other side: 111: Connection refused.
最佳答案
让我们稍微探索一下“吞”。 “吞下”异常是什么意思?
这是我认为最直接、最忠实的解释:
try:
user_code()
except:
pass
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.internet import protocol, reactor
class Broken(protocol.Protocol):
def connectionMade(self):
buggy_user_code()
e = TCP4ClientEndpoint(reactor, "127.0.0.1", 22)
f = protocol.Factory()
f.protocol = Broken
e.connect(f)
reactor.run()
connectionMade
实际上被调用),产生的输出是:
Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 674, in doConnect
self._connectDone()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 681, in _connectDone
self.protocol.makeConnection(self)
File "/usr/lib/python2.7/dist-packages/twisted/internet/protocol.py", line 461, in makeConnection
self.connectionMade()
File "/usr/lib/python2.7/dist-packages/twisted/internet/endpoints.py", line 64, in connectionMade
self._wrappedProtocol.makeConnection(self.transport)
File "/usr/lib/python2.7/dist-packages/twisted/internet/protocol.py", line 461, in makeConnection
self.connectionMade()
File "proderr.py", line 6, in connectionMade
buggy_user_code()
exceptions.NameError: global name 'buggy_user_code' is not defined
Deferred
交互。类(class)。既然你提到了
错误回复 我猜这就是咬你的情况。
Deferred
可以有一个成功的结果或一个失败的结果。当它有任何结果和更多的回调或 errback 时,它会尝试将结果传递给下一个回调或 errback。
Deferred
的结果然后成为调用这些函数之一的结果。尽快
Deferred
已经完成了所有的回调和 errback,它会保留它的结果,以防更多的回调或 errback 添加到它。
Deferred
最终得到一个失败结果并且没有更多的错误返回,那么它只是坐在那个失败上。如果它在处理该故障的 errback 添加到它之前被垃圾收集,那么它将记录异常。这就是为什么你应该总是在你的延迟上有错误回复,至少这样你可以及时记录意外的异常(而不是受到垃圾收集器的奇思妙想)。
Deferred
失败的结果并且没有错误返回来处理它。
e.connect(f)
Deferred
,但调用代码只是丢弃它。因此,它没有回调或错误返回。当它得到它的失败结果时,没有代码来处理它。该错误仅在
Deferred
时记录是垃圾收集,它发生在不可预测的时间。通常,特别是对于非常简单的示例,直到您尝试关闭程序(例如通过 Control-C)才会进行垃圾收集。结果是这样的:
$ python someprog.py
... wait ...
... wait ...
... wait ...
<Control C>
Unhandled error in Deferred:
Unhandled Error
Traceback (most recent call last):
Failure: twisted.internet.error.ConnectionRefusedError: Connection was refused by other side: 111: Connection refused.
twisted.internet.defer.setDebugging
可能会有所帮助。如果示例更改为使用它来启用
Deferred
调试:
from twisted.internet.defer import setDebugging
setDebugging(True)
exarkun@top:/tmp$ python proderr.py
... wait ...
... wait ...
... wait ...
<Control C>
Unhandled error in Deferred:
(debug: C: Deferred was created:
C: File "proderr.py", line 15, in <module>
C: e.connect(f)
C: File "/usr/lib/python2.7/dist-packages/twisted/internet/endpoints.py", line 240, in connect
C: wf = _WrappingFactory(protocolFactory, _canceller)
C: File "/usr/lib/python2.7/dist-packages/twisted/internet/endpoints.py", line 121, in __init__
C: self._onConnection = defer.Deferred(canceller=canceller)
I: First Invoker was:
I: File "proderr.py", line 16, in <module>
I: reactor.run()
I: File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1162, in run
I: self.mainLoop()
I: File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1174, in mainLoop
I: self.doIteration(t)
I: File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 140, in doSelect
I: _logrun(selectable, _drdw, selectable, method, dict)
I: File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
I: return callWithContext({"system": lp}, func, *args, **kw)
I: File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
I: return context.call({ILogContext: newCtx}, func, *args, **kw)
I: File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
I: return self.currentContext().callWithContext(ctx, func, *args, **kw)
I: File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
I: return func(*args,**kw)
I: File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
I: why = getattr(selectable, method)()
I: File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 638, in doConnect
I: self.failIfNotConnected(error.getConnectError((err, strerror(err))))
I: File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 592, in failIfNotConnected
I: self.connector.connectionFailed(failure.Failure(err))
I: File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1048, in connectionFailed
I: self.factory.clientConnectionFailed(self, reason)
I: File "/usr/lib/python2.7/dist-packages/twisted/internet/endpoints.py", line 144, in clientConnectionFailed
I: self._onConnection.errback(reason)
)
Unhandled Error
Traceback (most recent call last):
Failure: twisted.internet.error.ConnectionRefusedError: Connection was refused by other side: 111: Connection refused.
e.connect(f)
线作为此
Deferred
的原点给出- 告诉您应该添加错误回复的可能位置。
Deferred
添加错误回复。首先,至少要记录错误。
d = e.connect(f)
def errback(reason):
reason.printTraceback()
d.addErrback(errback)
from twisted.python.log import err
d = e.connect(f)
d.addErrback(err, "Problem fetching the foo from the bar")
Deferred
的想法有些基础。所以也不太可能改变。
Deferred
, 真正致命的错误必须停止您的应用程序,然后您可以定义一个合适的 errback 并将其附加到该
Deferred
:
d = e.connect(f)
def fatalError(reason):
err(reason, "Absolutely needed the foo, could not get it")
reactor.stop()
d.addErrback(fatalError)
关于python - 停止 Twisted 吞下异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9295359/
我正在尝试发送 RFC5424 中定义的结构化数据从 Twisted 到系统日志。我在 Ubuntu 上,系统日志实现是 rsyslog。 我正在使用“twistd --syslog”运行扭曲的应用程
摘要:无法自动向 Twisted 的检修孔发出命令。寻求解决方案/建议。 Twisted 有一个很棒的功能,称为 manhole。它允许用户通过 ssh 连接到当前正在运行的 Twisted 服务器并
我正在尝试发送 RFC5424 中定义的结构化数据从 Twisted 到系统日志。我在 Ubuntu 上,系统日志实现是 rsyslog。 我正在使用“twistd --syslog”运行扭曲的应用程
我正在尝试使用扭曲的 spawnProcess 启动一个进程,并使用 psutil 每 5 秒记录一次资源使用情况。首先,我尝试使用以下代码获取生成的进程的可执行文件名称: #!/usr/bin/py
Azure 网站能否托管 Twisted 应用程序?例如像这样的东西: from twisted.internet import reactor from twisted.web import ser
有没有办法限制 Twisted http 客户端的下载速率?如果没有,在 Twisted 中实现此类客户端的最简单方法是什么? 最佳答案 Twisted 中的流控制最常使用 IProducer.pau
我的基于 Twisted 的客户端循环发送 UDP 数据包。因此,我正在使用 DatagramProtocol 类。这是来源: #!/usr/bin/python # -*- coding: utf-
raven 有很多集成,包括 python 日志记录。一方面,twisted 不使用python 的日志记录。而另一方面,在twisted 中,raven 并没有直接的整合。 那么在基于扭曲的设置中使
有人告诉我,Twisted基于库Ampoule是创建在不同计算机上执行的进程池的好方法。然而,没有相关文档,Ampoule 的示例也没有说明这一点。 我对类似于 stdlib multiprocess
Twisted 最近推出了一个新的日志记录模块:twisted.logger。我已经通读了文档 [1],但我一直无法找到设置实际日志级别的位置。关于日志观察者[2] 的部分建议,如果您显式配置自己的
我想问一个关于如何关闭扭曲连接的问题 RPC . 我知道有人问过类似的问题,但似乎没有回答我的问题。 我正在做一些基本的连接,如下图所示: cfactory = pb.PBClientFactory(
我正在使用 Twisted 编写代码,但在为我的扭曲互联网延迟变量想出一个合理的变量名时遇到了麻烦。这是我的候选人: d :太通用,太短,违反了 pylint 规则 C0103。 def :与内置函数
我正在为 Web 服务器的静态部分使用 Twisted Web static.File 资源。 对于开发,我希望能够添加新文件或修改当前静态文件,而无需重新启动 Twisted 网络服务器。 我在 g
我想使用 Twisted 重建现有应用程序的通信部分。这个应用程序确实从客户端向服务器发送数据,只是这样,服务器不发送任何东西。 我如何使用 Twisted 的事件驱动概念来实现这一目标?我目前使用
我开始考虑通过扩展当前的 Twisted FTP 来实现显式 FTP。 大部分代码都很简单,实现 AUTH、PBSZ、PROT 很容易,我得到了一个有效的安全控制 channel 。 我的问题是数据通
我想实现 WebRTC 对等连接。我不知道如何设置 ICE 服务器或应该使用什么工具。任何人都可以给我建议吗?非常感谢。 最佳答案 实际上你不需要编写自己的 STUN/TURN 服务器 有很多现成的解
正如标题所示,ProcessProtocol 类上的这两个函数有什么区别?关于什么时候应该使用一种而不是另一种的文档有点稀疏? 我最好寻找一些可以证明这一点的用例示例。 最佳答案 我猜文档在这一点上有
使用下面的代码,我似乎可以相当轻松地使用 multiprocessing.reduction 在子进程中重建套接字.. import socket,os import multiprocessing
我正在尝试在 python 上使用 twisted 编写服务器。这是我文件的头部: from twisted.internet.protocol import Factory, Protocol fr
我使用 Twisted 制作了一个简单的 http 服务器,它发送 Content-Type: multipart/x-mixed-replace header 。我正在使用它来测试我想设置为接受长期
我是一名优秀的程序员,十分优秀!