[Twisted-Python] Amazing exception "GeneratorExit"

Christopher Armstrong radix at twistedmatrix.com
Fri Jan 25 12:49:09 EST 2013


On Fri, Jan 25, 2013 at 10:35 AM, Аркадий Левин <poisonoff at gmail.com> wrote:

> Hi all, i got strange error
>
> If run this:
>
> # -*- coding: UTF-8 -*-
>
> import os
> import sys
>
> from twisted.python import log
> from twisted.internet import reactor
> from twisted.internet.defer import Deferred, inlineCallbacks
>
> @inlineCallbacks
> def _():
>
>         try:
>                 (yield Deferred())
>         except:
>                 log.err()
>
> reactor.callLater(0, _)
> reactor.run()
>
> i got exception  "GeneratorExit" Why?!
>


This behavior isn't specific to inlineCallbacks. If you get rid of the
reactor usage and the @inlineCallbacks decorator, and just call _().next(),
you'll see the same behavior.

http://docs.python.org/2.7/reference/expressions.html?highlight=yield%20expression#generator-iterator-methods

See the stuff about how .close() throws GeneratorExit into the generator.

It is related to garbage collection. When a generator is garbage collected,
its .close() method is called. Since you're keeping a reference to the
Deferred in the generator itself in the working version, it's not being
immediately closed because there's a circular reference. (I think?)

-- 
Christopher Armstrong
http://radix.twistedmatrix.com/
http://planet-if.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20130125/910dc148/attachment.htm 


More information about the Twisted-Python mailing list