Forum: Ruby non-blocking gets in Windows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E8ae3db47865eaba38af9ded6ffd7069?d=identicon&s=25 Duckz King_duckz (king_duckz)
on 2009-01-06 17:33
Hello, I know this topic has already been discussed here a while ago,
but I was wondering if anyone came up with a workaround or something.
I'm trying to write a script that checks email every - say - 10 minutes,
while still accepting user's input, so that one can stop it.
This code works perfectly under linux:

def Prova1
running = true
th = Thread.new do
while running
inp = gets.chomp
running = false if ["QUIT", "EXIT"].include?(inp.upcase)
puts "Ho ricevuto \"#{inp}\"..."
end
end
until th.join(0.5)
puts "Attesa..."
sleep 1
end
nil
end

but if run under Windows, the whole script stops as soon as "gets" is
reached, as if it was single-threaded.
4feed660d3728526797edeb4f0467384?d=identicon&s=25 Bill Kelly (Guest)
on 2009-01-06 23:12
(Received via mailing list)
From: "Duckz King_duckz" <king_duckz@yahoo.it>
>
> but if run under Windows, the whole script stops as soon as "gets" is
> reached, as if it was single-threaded.

I believe this works in ruby 1.9.1, now that native threads are used.

(I tested IO.popen on 1.9.1 on windows a few days ago, and finally all
of ruby is not blocked by a gets on the pipe.  Yay!)

In ruby 1.8 and earlier, the trouble is that since ruby uses select() in
its thread scheduling, on windows, select() only works with sockets,
not other types of file handles.  (I.e. windows is broken.)


Regards,

Bill
This topic is locked and can not be replied to.