[Python-ideas] Cofunctions - Back to Basics

Steven D'Aprano steve at pearwood.info
Sat Oct 29 08:52:44 CEST 2011


Nick Coghlan wrote:

> PEP 3152 (and all generator based coroutines) have the limitation that
> they can't suspend if there's a *Python* function on the stack. Can
> you see why I know consider this approach categorically worse than one
> that pursued the Lua approach?

Can you give a concrete example of this problem? Even a toy example will 
do. The only thing I can think of is something like this:

def function(co):
     value = 1000
     for i in (1, 2, 3):
         value -= co.send(i)
     return value

def coroutine():
     value = (yield 1)
     while True:
         value += 1
         print("suspending...")
         value += (yield value)
         print("waking...")

 >>> co = coroutine()
 >>> co.send(None)
1
 >>> co.send(3)
suspending...
4
 >>> function(co)
waking...
suspending...
waking...
suspending...
waking...
suspending...
972
 >>> co.send(10)
waking...
suspending...
24


But as far as I understand it, that seems to show the coroutine 
suspending even though a function is on the stack. Can you explain what 
you mean and/or how I have misunderstood?



-- 
Steven



More information about the Python-ideas mailing list