[Twisted-Python] Strange error: SQL-Server tries to rollback
Paul Goins
general at vultaire.net
Fri Sep 10 10:39:13 MDT 2010
Thanks for the code example.
I can't offer to take this work over, but maybe I can make a comment.
There's one part that stands out to me.
> try:
> deferred = self.dbpool.runOperation(sql)
> #print("DATA sent")
> except Exception as e:
> print("error in insertDATA")
> print(e)
> return
> return deferred
It seems like maybe you have a misunderstanding about how Deferreds
generally work in Twisted. (Been there myself.) Basically, the above
try/except block won't work to catch errors from most Deferreds... well,
at least not without some extra magic.
You really should read the Deferred section of the Twisted documentation
to understand how errors are handled.
Docs are here:
http://twistedmatrix.com/documents/current/core/howto/defer.html
----
Basically, to "fix" the above code's error catching, you have two choices:
1. You can add an errback to the deferred. This is the "standard"
Twisted way, and would replace the try/except block entirely.
2. You can use the @inlineCallbacks decorator (from
twisted.internet.defer), and yield on the Deferred. This is easier, and
it allows try/except blocks, but there's some gotchas.
inlineCallbacks is what I used when I was learning Twisted, and you may
want to try that for now. But please understand that it hides details
about how Deferreds and callbacks really work. When you find time, read
the Deferred docs.
----
Best of luck,
- Paul Goins
More information about the Twisted-Python
mailing list