python - Twisted Deferred not displaying unhandled Exception without errback -
i'm reading through mckellar , fettig's twisted network programming essentials, 2nd ed.
i running twisted 15.5.0 on python 2.7.10 on windows 7.
in section deferred
there's example supposed raise unhandled error in deferred
- getting complete silence console when run minimal example below:
minimal example
from twisted.internet.defer import deferred def raiseerr(err): raise exception(err) d = deferred() d.addcallback(raiseerr) d.callback("oh no")
$ python test.py (no output)
minimal example actual book text
the actual example book along these lines:
from twisted.internet.defer import deferred def callback1(result): print "callback 1 said:", result return result def callback2(result): print "callback 2 said:", result def callback3(result): raise exception("callback 3") def errback1(failure): print "errback 1 had an error on", failure return failure d = deferred() d.addcallback(callback1) d.addcallback(callback2) d.addcallback(callback3) d.callback("test")
and expected output listed in book as:
callback3
raisesexception
, , because there no registerederrback
handleexception
, program terminates , reportsunhandled error
user. result is:
callback 1 said: test callback 2 said: test unhandled error in deferred: unhandled error traceback (most recent call last): file "/tmp/test.py", line 33, in <module> d.callback("test") <...> file "/tmp/test.py", line 11, in callback3 raise exception("callback 3") exceptions.exception: callback 3
am doing wrong?
edit:
i have gotten error display correctly on machine.
to enable error logged without having errback
handler on deferred
object, needed add following snippet:
import sys twisted.python import log log.startlogging(sys.stdout) # rest of code goes here
now, when run minimal example first code snippet in question, following output:
2016-02-05 09:45:43-0600 [-] log opened. 2016-02-05 09:45:43-0600 [-] invalid format string or unformattable object in log message: '%(log_legacy)s', {'format': '%(log_legacy)s', 'log_legacy': <twisted.logger._stdlib.stringifiablefromevent object @ 0x038913f0>, 'time': 1454687143.778, 'message': (), 'log_time': 1454687143.778, 'log_namespace': 'twisted.internet.defer', 'log_level': <loglevel=critical>, 'log_source': none, 'system': '-', 'iserror': true, 'log_logger': <logger 'twisted.internet.defer'>, 'log_format': 'unhandled error in deferred:'} 2016-02-05 09:45:43-0600 [-] unhandled error traceback (most recent call last): file "testd.py", line 13, in <module> d.callback("oh no") file "c:\swat\.virtualenvs\twisted\lib\site-packages\twisted\internet\defer.py", line 393, in callback self._startruncallbacks(result) file "c:\swat\.virtualenvs\twisted\lib\site-packages\twisted\internet\defer.py", line 501, in _startruncallbacks self._runcallbacks() --- <exception caught here> --- file "c:\swat\.virtualenvs\twisted\lib\site-packages\twisted\internet\defer.py", line 588, in _runcallbacks current.result = callback(current.result, *args, **kw) file "testd.py", line 9, in raiseerr raise exception(err) exceptions.exception: oh no
so, can verify twisted indeed raise error meant - didn't feel telling me reason. if can elaborate why default case handling exception without errback defined, i'd love know.
i've changed title reflect new question.
i found question after hitting similar issue whilst working way through dave perticola's excellent twisted introduction in part 9: second interlude, deferred has example similar 1 have posted:
from twisted.internet.defer import deferred def callback(res): raise exception('oops') d = deferred() d.addcallback(callback) d.callback('here result.') print "finished"
just did not output suggested:
finished unhandled error in deferred: traceback (most recent call last): ... --- <exception caught here> --- ... exceptions.exception: oops
i assumed versioning issue, twisted 16 pypy 5.0.1 on el6 me, research led me here; , when turning on logging, suggested in answer, had no effect on think actual answer.
the clue answer in tutorial working statement of:
the reason “finished” comes first because “unhandled” message isn’t printed until deferred garbage collected.
for reason, , i, time garbage collector gets around doing thing no longer has anywhere output to. if want see true, here modified code:
import gc twisted.internet.defer import deferred def callback(res): raise exception('oops call') d = deferred() d.addcallback(callback) d.callback('here result.') d = none gc.collect() print "finished"
i guess real answer deal exceptions before garbage collector gets them, perhaps bit of info let sleep bit easier if don't know why works some, know causing our expected error messages disappear.
Comments
Post a Comment