473,382 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

generator function within a generator function doesn't execute?

<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
Jul 18 '05 #1
1 1893
th**********@aol.com (TheDustbustr) wrote in message news:<20***************************@mb-m17.aol.com>...
<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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
11
by: vbgunz | last post by:
I am afraid that this is the first time in which I would probably need something explained to me as if I were a little child. I am having a hard time getting this through my thick skull. What in...
3
by: Paul Rubin | last post by:
As I understand it, generators are supposed to run til they hit a yield statement: import time def f(): print 1 time.sleep(3) for i in range(2,5): yield i
14
by: castironpi | last post by:
I'm actually curious if there's a way to write a generator function (not a generator expression) in C, or what the simplest way to do it is... besides link the Python run-time.
1
by: Blubaugh, David A. | last post by:
Pauli, Yes, I am utilizing the windows environment. I cannot install f2py. I obtain the following error when I try to execute the setup.py file within the f2py folder located within the...
3
by: Max Vit | last post by:
I have come across a strange issue whilst trying to use a shell command call from Access and have spent some time trying to figure this out but can't find the cause as yet. The issue is: I need...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.