generator function within a generator function doesn't execute?

Discussion in 'Python' started by TheDustbustr, Jul 25, 2003.

  1. TheDustbustr

    TheDustbustr Guest

    <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
     
    TheDustbustr, Jul 25, 2003
    #1
    1. Advertising

  2. (TheDustbustr) wrote in message news:<>...
    > <code>

    <snip>
    > </code>


    > 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.


    Calling sleep(2.5) isn't supposed to run anything. It just creates a
    generator object. Calling it from within another generator makes no
    difference. You need to manually unwind the sleep generator to your
    scheduler, so instead of

    sleep(2.5)

    do

    for x in sleep(2.5): yield x
     
    Sami Hangaslammi, Jul 25, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Hammond
    Replies:
    2
    Views:
    4,525
    bruce barker
    Nov 14, 2003
  2. Jill Graham
    Replies:
    2
    Views:
    338
    Jill Graham
    Mar 4, 2004
  3. lord0
    Replies:
    0
    Views:
    890
    lord0
    Apr 7, 2004
  4. Replies:
    2
    Views:
    2,020
  5. Savas Ates
    Replies:
    1
    Views:
    287
    Patrice
    Aug 17, 2004
Loading...

Share This Page