librelist archives

« back to archive

Does Shoes execute instructions in order, or what?

Does Shoes execute instructions in order, or what?

From:
Jesse Edelstein
Date:
2012-02-09 @ 19:05
Hi again. Hope this isn't too spammy, but this community has been very
helpful so far. Yet the more I get into Shoes the dumber I feel. My
testing suggests that Shoes is running my code in an unpredictable
order due to multithreading or something; at any rate, it seems like
if I run method1, then method2 there's no guarantee that method1 will
return before method2 starts, which is driving me a little bit crazy
as it seems to contradict what I'm used to in Ruby. Am I way off base
with this?

I certainly could be, but I can't think of any other explanation for
why my app won't run like I want. I uploaded the whole thing here:
http://pastebin.com/61HUPjjT and I would be super grateful to any
Rubyists who feel like taking a look. Right now I have the
PresentInstructions class working okayish, but MultipleUsesTask is
nonfunctional because it doesn't display the prompt and editbox when
it's supposed to - instead the timer just counts down without
displaying anything. (That said, don't worry too much about the actual
application - it's a cognitive psychology experiment that's sort of
intended to be boring, because we want to elicit mind wandering for
our perverse reasons...) If you do take a look at the app, could you
try to give a quick explanation for why it's not working as intended,
and suggest how I could fix it? I've been staring down these problems
for hours now, and getting nowhere.

Cheers, Jesse

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
Steve Klabnik
Date:
2012-02-09 @ 20:34
Hope this isn't too spammy, but this community has been very
> helpful so far.

Absolutely not! Please ask tons of questions, we prefer more threads
than not. Busy-ness is good!

> Yet the more I get into Shoes the dumber I feel. My
> testing suggests that Shoes is running my code in an unpredictable
> order due to multithreading or something; at any rate, it seems like
> if I run method1, then method2 there's no guarantee that method1 will
> return before method2 starts, which is driving me a little bit crazy
> as it seems to contradict what I'm used to in Ruby. Am I way off base
> with this?

Much GUI code is multi-threaded, yes. Shoes is no different. That
said, I'd need an example...

> I certainly could be, but I can't think of any other explanation for
> why my app won't run like I want. I uploaded the whole thing here:
> http://pastebin.com/61HUPjjT and I would be super grateful to any
> Rubyists who feel like taking a look

Bam! Let me check it out.

Hrm, on my mac it doesn't really do anything. The console says
"presenting instructions..." and then... nothing. I'm on a Mac, 10.7,
so it seems that this agrees with your comments. And without it
working, it's hard to play around with it... let's see what others
have to say.

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
Jesse Edelstein
Date:
2012-02-09 @ 20:51
Thanks for checking it out, Steve. If you want a really
straightforward example, how about:

Shoes.app do
	p = para 'Wait for it...'
	sleep 5
  	p.replace 'Hooray!'
end

Runs without errors, but on my OS X 10.7 it doesn't work like I
expected intuitively - it waits 5 seconds, then prints Hooray! I think
I'd be on a lot firmer ground if I knew how to make Shoes execute each
of the three steps in the order I wrote them.

As for my app, you're getting pretty much what I see. After I hit
space to continue past the first instructions, it's supposed to go to
the next task, giving the user a chance to freely enter some
responses; however, it just sits there for thirty seconds because it's
running a timer instead of drawing the new UI elements. Originally,
line 71 only said collect_remaining_entry with no timer involved;
unfortunately this led to that method being run first, before the time
ran out for free responses (and I don't understand why.) My main goal
now is to figure out how to more closely control the execution of code
- what can I do to help Shoes see it more my way?

Jesse

On Thu, Feb 9, 2012 at 12:34 PM, Steve Klabnik <steve@steveklabnik.com> wrote:
> Hope this isn't too spammy, but this community has been very
>> helpful so far.
>
> Absolutely not! Please ask tons of questions, we prefer more threads
> than not. Busy-ness is good!
>
>> Yet the more I get into Shoes the dumber I feel. My
>> testing suggests that Shoes is running my code in an unpredictable
>> order due to multithreading or something; at any rate, it seems like
>> if I run method1, then method2 there's no guarantee that method1 will
>> return before method2 starts, which is driving me a little bit crazy
>> as it seems to contradict what I'm used to in Ruby. Am I way off base
>> with this?
>
> Much GUI code is multi-threaded, yes. Shoes is no different. That
> said, I'd need an example...
>
>> I certainly could be, but I can't think of any other explanation for
>> why my app won't run like I want. I uploaded the whole thing here:
>> http://pastebin.com/61HUPjjT and I would be super grateful to any
>> Rubyists who feel like taking a look
>
> Bam! Let me check it out.
>
> Hrm, on my mac it doesn't really do anything. The console says
> "presenting instructions..." and then... nothing. I'm on a Mac, 10.7,
> so it seems that this agrees with your comments. And without it
> working, it's hard to play around with it... let's see what others
> have to say.

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
Steve Klabnik
Date:
2012-02-09 @ 21:15
Right. This is the kind of stuff that happens when doing GUI stuff in
a multithreaded environment...

Try this, for example:


app = Shoes.app do
  def p
    @p
  end

  @p = para 'Wait for it...'
end

Thread.new do
    sleep 5
    app.p.replace 'Hooray!'
end

See the difference? Threads are hard. :/

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
Jesse Edelstein
Date:
2012-02-09 @ 21:21
Yeah, I definitely see what you mean. Looks like I'm in for some
refactoring and some reading. Does anyone have some recommendations
for an introduction to multithreading, beyond what the Pickaxe
provides?

On Thu, Feb 9, 2012 at 1:15 PM, Steve Klabnik <steve@steveklabnik.com> wrote:
> Right. This is the kind of stuff that happens when doing GUI stuff in
> a multithreaded environment...
>
> Try this, for example:
>
>
> app = Shoes.app do
>  def p
>    @p
>  end
>
>  @p = para 'Wait for it...'
> end
>
> Thread.new do
>    sleep 5
>    app.p.replace 'Hooray!'
> end
>
> See the difference? Threads are hard. :/

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
Steve Klabnik
Date:
2012-02-09 @ 21:29
I'm not sure, just tweeted.

Note that usually, this isn't needed: it's because you want it to
start automatically. Other Shoes apps basically do this:


Shoes.app do
  @p = para 'Wait for it...'

  button "Start experiment" do
    sleep 5
    @p.replace 'Hooray!'
  end
end


Sleep-ing will still block the event loop, though, so it's easier to
make a thread there, too:


Shoes.app do
  @p = para 'Wait for it...'

  button "Start experiment" do
    Thread.new do
      sleep 5
      @p.replace 'Hooray!'
    end
  end
end

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
ashbb
Date:
2012-02-10 @ 00:26
Hi Steve and Jesse,

> Sleep-ing will still block the event loop, though, so it's easier to
> make a thread there, too:
Yeah, Steve's snippet works well.

But Shoes has a `timer` method. So, you can write the same code like this:

Shoes.app do
  @p = para 'Wait for it...'
  button "Start experiment" do
    timer 5 do
      @p.replace 'Hooray!'
    end
  end
end

ashbb

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
ashbb
Date:
2012-02-10 @ 02:14
Hi Jesse,

> I uploaded the whole thing here: http://pastebin.com/61HUPjjT
Thanks for sharing a curious code.

I edited a little bit: http://pastebin.com/QzLZnvD4

Try it out. ;-)

ashbb

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
Jesse Edelstein
Date:
2012-02-16 @ 20:59
Thank you! I can't quite figure out what you changed from the diff,
but hey, it works when before it didn't. If I ever publish this stuff,
I'll have to remember to put you in the thank-yous.

I have another code issue - I implemented the SART task (the layout is
unfinished but I think all the logic is there) but now I can't get the
instructions stack for this task to go away. I mean, I wrote this bit
of vindictive code to try and thoroughly remove the stack, but it
seems to have no effect, and the SART Task somehow runs in the
background, not displaying its cues:

    close_this_thing = Thread.new do
      @instructional_display.remove
      @space_detector.remove
      @s.remove
    end
    close_this_thing.join

I mean, is it possible for Shoes to execute that part of the code but
not remove those elements from the app? Maybe it's not getting called
at the right time but I can't figure out why that would be. Anyway I
would really appreciate a quick code audit from anyone who's willing:
http://pastebin.com/AkT3QE23 I hate to ask for so much help instead of
figuring it out myself, but I've got people coming to do this study in
an hour. :P

Cheers, Jesse

On Thu, Feb 9, 2012 at 6:14 PM, ashbb <ashbbb@gmail.com> wrote:
> Hi Jesse,
>
>
>> I uploaded the whole thing here: http://pastebin.com/61HUPjjT
> Thanks for sharing a curious code.
>
> I edited a little bit: http://pastebin.com/QzLZnvD4
>
> Try it out. ;-)
>
> ashbb

Re: [shoes] Does Shoes execute instructions in order, or what?

From:
ashbb
Date:
2012-02-17 @ 12:33
Hi Jesse,

I downloaded http://pastebin.com/AkT3QE23 and saved to orig.rb.
Then edited a little bit and saved to revised.rb.
The revised.rb works on my Windows 7 with Shoes 3. ;-)

The following is the diff.

D:\tmp>diff orig.rb revised.rb
134a135
>     @step = order_in_experiment
182c183
<               sleep 0.5
---
>               #sleep 0.5
191c192
<               sleep 0.5
---
>               #sleep 0.5

BTW. Do you know `Shoes.url`?
This is a sample snippet: https://gist.github.com/1853092

In order to implement what you want to do in Shoes,
it may be easier than using thread and sleep.

ashbb