From: "Michael W. Ryder" <_mwryder@...> Date: 2006-12-18T10:40:05+09:00 Subject: Re: Question regarding threads and I/O Logan Capaldo wrote: > On Mon, Dec 18, 2006 at 08:55:10AM +0900, Michael W. Ryder wrote: >> I am a veteran programmer but still am trying to learn Ruby. In the >> language I have been using for over 25 years input has an option to >> terminate after x number of seconds, something that seems to be missing >> in Ruby. I use this ability to log off sessions when the user leaves >> the computer for a time along with other purposes. >> Is it possible in Ruby to use two threads for input, one for reading >> from the selected source and the second that would terminate the first >> thread if it hadn't completed in the specified amount of time? The main >> program would start the first thread and feed the PID to the second >> thread along with the number of seconds, or milliseconds, to sleep. The >> second thread would then see if the first thread is still alive when it >> wakes up and if so terminate it. Either way the second thread would >> exit after the number of seconds given. > Check out Timeout in the standard lib. > http://www.ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html > Maybe I am missing something here but it doesn't seem to work for me. I have the following simple program: require 'timeout' foo = nil status = Timeout::timeout(5) { foo = gets() } puts (foo) If I enter something, say 123, then the program displays the 123 as expected. If I just wait it sits there until I enter something which is not what I want. I want the input to terminate and display that foo is 'nil'. For some reason testing the value of status shows that it is the same as the input, but this may not be relevant to the problem.