From: Kristof Bastiaensen Date: 2004-08-30T04:50:39+09:00 Subject: Re: Blocking Problem On Sun, 29 Aug 2004 07:28:59 +0900, Zachary P. Landau wrote: Hi, Since you are using Gtk, maybe the Gdk::Input.add method could help you. You can invoke it like this: Gdk::Input.add(my_io.to_i, Gdk::Input::Condition::READ) do #block to execute when data is ready in the io end This is the version for gtk2, the gtk (1.2) version is a little different. You must have a Gtk.main loop for this to work. I hope this is helpful. Regards, KB > > Hello, > > I'm having trouble with IO blocking when reading from a device. It may be > hard for people to help since most people won't be able to test this > themselves, but I'll give it a shot. > > I'm trying to read from the CueCat Linux driver, which sits in > /dev/scanner/cuecat. It is sort of like a pipe, you can use File.read and > it outputs a line with a barcode (and some other information). > > The problem is, I can't avoid blocking. There is a GTK application, and > then a separate thread that tries to read barcodes and whenever it finds > one, it tells the GTK app. > > So my code looks like this: > > Thread.new do > File.open(CUECAT_DEV, File::RDONLY | File::NONBLOCK) do |cuecat| > loop do > id, barcode_type, barcode = cuecat.gets.split(/,/) # do stuff with > the barcode > end > end > end > > When I use a named pipe instead of CUECAT_DEV, it doesn't block. But when > I use the actual /dev/scanner/cuecat, it blocks. Unfortunately, blocking > locks up the entire application. > > I tried getting around the issue by using Gtk.timeout_add instead, but > that didn't help. > > The mailing list has some posts about blocking problems, but they are > related to sockets and pipes. As I mentioned earlier, the above code > works when CUECAT_DEV is a pipe.