[PyPy-issue] [issue693] Documentation needed: Coroutine garbage collection

Chris Smowton pypy-dev-issue at codespeak.net
Tue Apr 26 10:40:57 CEST 2011


Chris Smowton <cs448 at cam.ac.uk> added the comment:

Nearly, but I'm not sure the "executed only after the .kill has finished" bit is
true! Here's an example session:

>>>> 
>>>> 
>>>> import gc
>>>> import stackless
>>>> 
>>>> def f():
....    try:
....       print "Start F!"
....       main_coro.switch()
....       print "End F!"
....    except CoroutineExit:
....       print "F: killed; switching to main"
....       main_coro.switch()
....       print "F: back in EH?"
.... 
>>>> def g():
....       print "Start G!"
....       main_coro.switch()
....       print "End G!"
.... 
>>>> main_coro = stackless.coroutine.getcurrent()
>>>> coro1 = stackless.coroutine()
>>>> coro1.bind(f)
>>>> coro2 = stackless.coroutine()
>>>> coro2.bind(g)
>>>> 
>>>> coro1.switch()
Start F!
>>>> coro2.switch()
Start G!
>>>> del coro1
>>>> gc.collect()
0
>>>> coro2.switch()
F: killed; switching to main
>>>> coro2.switch()
End G!
>>>> 

Because F catches the CoroutineExit exception, we *never* end up in coro2! If
there's any possibility a coroutine will be GC'd and will catch that call, the
programmer will need to instrument their coroutines to figure out what actually
got executed and re-issue the coro2.switch() at the next opportunity.

This doesn't seem good -- if there's any possibility a coroutine might become
garbage, the programmer will need to instrument their coroutines in order to
work out that this happened and re-issue the switch. I'm not sure what the right
thing to do would be here?

_______________________________________________________
PyPy development tracker <pypy-dev-issue at codespeak.net>
<https://codespeak.net/issue/pypy-dev/issue693>
_______________________________________________________



More information about the Pypy-issue mailing list