On Jul 6, 2016, at 8:47 PM, Glyph Lefkowitz <glyph@twistedmatrix.com> wrote:


On Jul 6, 2016, at 5:25 PM, Yury Selivanov <yselivanov@gmail.com> wrote:

The problem is that the GC can’t execute async code, and we don’t have any control over GC.  What if we add a mechanism to control how async generators (AG) are destructed.  Let’s say we add new function to the sys module - `sys.set_async_generator_finalizer(finalizer)`.  We already have sys.set_coroutine_wrapper(), so this isn’t something unprecedented.

There isn't just one event loop though, and what trampoline to attach a dying coroutine to depends heavily on what event loop it came from.  It seems like a single global value for this in 'sys' would just be ... wrong.

But there can only be one currently running event loop per thread...

Another way is to add sys.set_async_generator_wrapper() (and a getter, so that loops can maintain a stack of them).  With it a running event loop can create a weak ref to a generator that is created in a coroutine that the loop is currently running. And with a weakref it can later finalize the generator.

Yury