while loops in bound functions

已查看 23 次
跳至第一个未读帖子

Jamie Kellogg

未读,
2017年11月7日 12:22:142017/11/7
收件人 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

未读,
2017年11月7日 16:31:462017/11/7
收件人 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

未读,
2017年11月7日 16:39:012017/11/7
收件人 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

未读,
2017年11月7日 16:43:562017/11/7
收件人 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

未读,
2017年11月7日 17:26:202017/11/7
收件人 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

--
回复全部
回复作者
转发
0 个新帖子