while loops in bound functions

23 views
Skip to first unread message

Jamie Kellogg

unread,
Nov 7, 2017, 12:22:14 PM11/7/17
to Glowscript Users
Howdy again.  I have run into a problem using while loops inside bound functions.  An example code snippet follows:

scene.height=200
scene.width=200
def test(b):
    print('Testing')
    while True:
        rate(5)
        print('hi')
b = button(text="Run", bind=test)

This code produces no output or error messages when run.  If I replace the while loop with a for loop, everything is great.  Any thoughts?
Thanks in advance,
Jamie


Bruce Sherwood

unread,
Nov 7, 2017, 4:31:46 PM11/7/17
to Glowscript Users
I've only recently realized that an event-driven function cannot contain any pause-like statements, which includes rate() statements. This is closely linked to the fact that JavaScript programs (to which VPython compiles) must be written in an "asynchronous" manner, such that functions must do their job quickly and then return control to the browser rather than containing long-running loops that lock up the browser. The GlowScript machinery uses the Streamline library to restructure programs that contain pauses (including rate) into an asynchronous form. For example, in a while loop containing a rate() statement, the contents of the loop are moved into a separate function that is called at a frequency determined by the rate argument. It turns out that this scheme isn't working for cases where a function containing pauses is called as an event-bound function.

While I would like to make it possible to put a pause in an event-bound function, and I will try, it is the case that the "asynchronous" nature of event-bound functions means that they are intended to perform some service and exit promptly, so that other processes can continue. It is conceivable that there isn't a way to make this work as one would like.

Bruce

Jamie Kellogg

unread,
Nov 7, 2017, 4:39:01 PM11/7/17
to glowscri...@googlegroups.com
Thank you for the quick reply, Bruce.  I'll see what I can do to work around this constraint  -- something like the old polling solution from earlier versions of vpython, maybe.
Best,
Jamie

--

---
You received this message because you are subscribed to a topic in the Google Groups "Glowscript Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/glowscript-users/egz3K3kIRCo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to glowscript-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bruce Sherwood

unread,
Nov 7, 2017, 4:43:56 PM11/7/17
to Glowscript Users
A simple scheme is to have a while loop that watches for a flag that is set by the event-driven loop.

Bruce

Jamie Kellogg

unread,
Nov 7, 2017, 5:26:20 PM11/7/17
to glowscri...@googlegroups.com
Just what I was thinking.  
Thanks again!
Jamie

On Tue, Nov 7, 2017 at 1:43 PM, Bruce Sherwood <bruce.s...@gmail.com> wrote:
A simple scheme is to have a while loop that watches for a flag that is set by the event-driven loop.

Bruce

--
Reply all
Reply to author
Forward
0 new messages