From: Chris Hulan Date: 2013-01-16T13:41:45+09:00 Subject: Re: Ruby Threads and Shoes --14dae9cdc6bd4d151904d360223d Content-Type: text/plain; charset=ISO-8859-1 Did you see this? http://stackoverflow.com/questions/958662/shoes-and-heavy-operation-in-separate-thread It uses an .append method, maybe that has built in smarts to allow the GUI to update Tried to have a look but th docs are as funky as _why's manual ;) BTW, having a rescue that just re-raises is redundant On Tue, Jan 15, 2013 at 9:26 PM, Steve L. wrote: > TL;DR: How do you pass execution from within a PTY.spawn block back to > the calling thread long enough to allow the caller to update GUI > elements? > > > I have a situation that I doubt is particularly unique, but Google > hasn't been a lot of help, so I thought I'd try you guys as a last > resort. So...to jump right in to it: > > I've built a front-end GUI for running Cucumber tests using Shoes. All > the app really does is take user selections from the GUI and use them to > string together a command line that I'm then passing in to it's own > process through a PTY.spawn call. > > The point of using the PTY library was to be able to grab the STDOUT > stream of the child process in real time, as per this stack-overflow: > > http://stackoverflow.com/questions/1154846/continuously-read-from-stdout-of-external-process-in-ruby > > When testing the bare-bones of my code in a terminal, it works > beautifully...the external process is spawned, and I'm able to pull the > stdout of the external process and see it in real time. However, my > situation has the added layer of a GUI. Despite the fact that I'm sure > that the IO pipes are flowing properly, I'm still not getting an update > on my GUI until after the child process terminates. > > I've done enough reading to understand that this has something to do > with the way Ruby handles thread priority, but I'm having a little bit > of trouble groking a potential solution to my problem. In short, what > I'm looking for is a way to 'give back' execution to my GUI process long > enough to let it update itself from the STDOUT output of the child > process. > > I hope I was clear enough, but feel free to ask clarifying questions, > and I appreciate any insight! > > My code, for reference: > > def spawn_command_in_pty(cmd, env, outwindow) > env.each do |k, v| > ENV[k] = v > end > PTY.spawn(cmd) do |r, w, pid| > begin > r.each{|line| outwindow.text = outwindow.text + line } > rescue Errno::EIO => e > raise e > end > end > > end > > Steve > > -- > Posted via http://www.ruby-forum.com/. > > --14dae9cdc6bd4d151904d360223d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Did you see this?=A0http://stackov= erflow.com/questions/958662/shoes-and-heavy-operation-in-separate-thread

It uses an .append method, maybe that has built in sma= rts to=A0allow=A0the GUI to update
Tried to have a look but= th docs are as funky as _why's manual ;)

BTW, having a rescue that just re-raises is redundant


On Tue, Jan 15, 2013 at= 9:26 PM, Steve L. <lists@ruby-forum.com> wrote:
TL;DR: How do you pass execution from within= a PTY.spawn block back to
the calling thread long enough to allow the caller to update GUI
elements?


I have a situation that I doubt is particularly unique, but Google
hasn't been a lot of help, so I thought I'd try you guys as a last<= br> resort. =A0So...to jump right in to it:

I've built a front-end GUI for running Cucumber tests using Shoes. All<= br> the app really does is take user selections from the GUI and use them to string together a command line that I'm then passing in to it's own=
process through a PTY.spawn call.

The point of using the PTY library was to be able to grab the STDOUT
stream of the child process in real time, as per this stack-overflow:
http://stackoverflo= w.com/questions/1154846/continuously-read-from-stdout-of-external-process-i= n-ruby

When testing the bare-bones of my code in a terminal, it works
beautifully...the external process is spawned, and I'm able to pull the=
stdout of the external process and see it in real time. =A0However, my
situation has the added layer of a GUI. =A0Despite the fact that I'm su= re
that the IO pipes are flowing properly, I'm still not getting an update=
on my GUI until after the child process terminates.

I've done enough reading to understand that this has something to do with the way Ruby handles thread priority, but I'm having a little bit<= br> of trouble groking a potential solution to my problem. =A0In short, what I'm looking for is a way to 'give back' execution to my GUI pro= cess long
enough to let it update itself from the STDOUT output of the child
process.

I hope I was clear enough, but feel free to ask clarifying questions,
and I appreciate any insight!

My code, for reference:

def spawn_command_in_pty(cmd, env, outwindow)
=A0 =A0 =A0 env.each do |k, v|
=A0 =A0 =A0 =A0 ENV[k] =3D v
=A0 =A0 =A0 end
=A0 =A0 =A0 PTY.spawn(cmd) do |r, w, pid|
=A0 =A0 =A0 =A0 begin
=A0 =A0 =A0 =A0 =A0 r.each{|line| outwindow.text =3D outwindow.text + line = }
=A0 =A0 =A0 =A0 rescue Errno::EIO =3D> e
=A0 =A0 =A0 =A0 =A0 raise e
=A0 =A0 =A0 =A0 end
=A0 =A0 =A0 end

=A0 =A0 end

Steve

--
Posted via http://= www.ruby-forum.com/.


--14dae9cdc6bd4d151904d360223d--