generator within a generator doesn't execute?

TheDustbustr thedustbustr at aol.com
Fri Jul 25 09:03:37 EDT 2003


from __future__ import generators
from time import time

threads=[]

def xsleep(n):
    t=time()
    while (t+n>time()): 
        yield None

def sleep(n):
    for i in xsleep(n): pass

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(3)
    #t=time()
    #while (t+3>time()): yield None

    print "\nEnd of cutscene."

def ticker():
    yield None #wait till second time through
    print "."
    t=time()
    while 1:
        while (t+.5>time()): yield None
        t=time()
        #sleep(.5)
        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()

I added xsleep() (a generator) and modified sleep() (now, not a generator). 
cutscene() works as expected.  Why does sleep() block execution of ticker()? 
Shouldn't xsleep() yield to the scheduler to allow ticker() to run?

Is there any way, save using Stackless Python microthreads, to make the sleep
function 'just work' transparently?





More information about the Python-list mailing list