From: Robert Klemme Date: 2004-02-25T21:39:52+09:00 Subject: Re: io/nonblock - blocks w/threads? "Ara.T.Howard" schrieb im Newsbeitrag news:Pine.LNX.4.44.0402242241480.28543-100000@fattire.ngdc.noaa.gov... > > this program does not work as expected w/o inserting the 'Thread.critical' > bits - i must say i do not understand why, can someone help me understand? > > require 'tk' > require 'open3' > require 'io/nonblock' > $VERBOSE=nil > > r=TkRoot.new > l=TkLabel.new r, :text=>`ruby -e "p Time.now"` > l.pack > > command='ruby -e "loop{p Time.now; sleep 1}"' > i,o,e = Open3::popen3 command > i.close > text = nil > > Thread.new do > loop do > rios, = select [o,e], nil, nil > rios.map do |rio| Why do you use map here? Apparently you don't access the result of mapping so each would be more appropriate. > next if rio.eof? > Thread.critical = true # blocks w/o this > rio.nonblock{ text = rio.read } > Thread.critical = false # blocks w/o this > l.configure :text=>text > end > end > end > > Tk.mainloop > > > is this an o.k. technique to prevent a non-blocking read not to hand a > multi-threaded ap? I'm not really sure I understand correctly what you're after. Could you elaborate that? Generally speaking there's one piece of advice: When using Thread.critical= it is always best to do reset the flag in an ensure clause to make sure that the reset occurs under all conditions: Thread.critical = true begin # do stuff ensure Thread.critical = false end > i'm think of adding an asychronous processing option to my > session library using this feature... actually i suppose it'd always be o.k. > for this to be there... Regards robert