generator function within a generator function doesn't execute?

TheDustbustr thedustbustr at aol.com
Thu Jul 24 19:09:41 EDT 2003


<code>
from __future__ import generators
from time import time
threads=[]

def sleep(n):
    print "In Sleep"
    t=time()
    while (t+n>time()): 
        yield None

def cutscene():
    yield None #start on second pass

    print "\nEvil Death Knight: I will kill you now!"

    sleep(1.5)
    #t=time()
    #while (t+1.5>time()): yield None

    print "\nOur Hero: I shall fight you to the death.  Bring it on!"

    sleep(2.5)
    #t=time()
    #while (t+2.5>time()): yield None

    print "\nEnd of cutscene."

def ticker():
    yield None #wait till second time through
    print "."
    t=time()
    while 1:
        while (t+1>time()): yield None
        t=time()
        print "."

def scheduler():
    global threads
    try:
        while 1:
            for thread in threads: thread.next()
    except StopIteration:
        pass
        
if __name__=="__main__":
    threads.append(ticker())
    threads.append(cutscene())
    scheduler()
</code>

ticker() prints out a period every second.  cutscene() is a scripted game
sequence.  sleep(n) should cause a delay of n seconds (but doesn't block
execution of the other microthreads (correct term?)).  ticker() and cutscene()
should be running simultaneously (in effect, at least).

However, when run, the sleep function doesn't execute.  It doesn't even print
"In Sleep".  It never gets called, even though I say sleep(1.5) in cutscene(). 
Run it for yourself if you don't believe me.

Through my testing, any generator function will not execute if called from
another generator function. Regular functions work fine.

If I comment the sleep statements and uncomment the while loops, it runs as
expected.  Why doesn't the sleep() function execute?

Thanks in advance.
Dustin




More information about the Python-list mailing list