Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 125213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:07:24+00:00 2026-05-11T05:07:24+00:00

I’m looking for a portable interface to POSIX alarm(2) (or similar) in Ruby. That’s

  • 0

I’m looking for a portable interface to POSIX alarm(2) (or similar) in Ruby. That’s to say, I would like to be able to set a background timer to send a signal to the current process after n seconds.

I have found some good discussion from 2006 on the ruby-talk list that provides a solution using dl/import, but that’s a bit of a hack (albeit a neat hack) and not very portable.

I’ve looked at the much-maligned Timeout module and that won’t cut it under JRuby although it works fine with the traditional interpreter. My program is a small command-line shell that uses the Readline library:

TIMEOUT = 5 # seconds loop do   input = nil   begin     Timeout.timeout(TIMEOUT) do       input = Readline::readline('> ', nil)     end   rescue Timeout::Error     puts 'Timeout'     next   end   # do something with input end 

Under JRuby it seems the process blocks in the readline call and Timeout::Error is only thrown after (a) the timer expires and (b) the user enters a new line. And the exception doesn’t get rescued. Hmm.

So I came up with this workaround:

require 'readline' class TimeoutException < Exception ; end TIMEOUT = 5 # seconds  loop do   input = nil   start_time = Time.now   thread = Thread.new { input = Readline::readline('> ', nil) }   begin     while thread.alive? do       sleep(1) # prevent CPU from melting       raise TimeoutException if(Time.now - start_time > TIMEOUT)     end   rescue TimeoutException     thread.exit     puts 'Timeout'   end   # do something with input end 

This is… clunky (let’s be polite). I just want alarm(2)! I don’t really want to drag in non-core libraries (eg Terminator) for this. Is there a better way?

EDIT: I can’t get another alternative — creating a thread that sleeps and then sends a signal to the process — to work under JRuby either. Does JRuby eat signals? Example:

SIG = 'USR2' Signal.trap(SIG) { raise } Process.kill(SIG, Process.pid) 

JRuby simply returns, Ruby returns the expected ‘unhandled exception’ error.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:07:24+00:00Added an answer on May 11, 2026 at 5:07 am

    I’m sorry that I don’t have an answer to your larger problem of sending a signal after X seconds to a process, but it seems that all you want to do is timeout after X seconds of waiting for input, and if that’s the case then I’d say you are looking for Kernel.select 😀

    I’ve personally never used this, but after doing a google for ‘non-blocking gets’, and subsequently exploring links, I found these two to be invaluable discussions:

    http://www.ruby-forum.com/topic/126795 (Discussion of multi-threaded gets)

    http://www.ruby-forum.com/topic/121404 (Explanation of Kernel.select in 2nd post)

    Here’s a sample of how to use it. This will print out your prompt and wait for input… If there is no input after five seconds, then the program will end. If there is input, as soon as there is input it will spit it back out and end… Obviously you can modify this for your own purposes.

    def prompt   STDOUT.write '> '   STDOUT.flush end  def amusing_messages   [ 'You must enter something!',      'Why did you even start me if you just wanted to stare at me?',      'Isn't there anything better you could be doing?',      'Just terminate me already... this is getting old',     'I'm waiting...'] end  prompt  loop do   read_array, write_array, error_array = Kernel.select [STDIN], nil, nil, 5    if read_array.nil?     puts amusing_messages[rand(amusing_messages.length)]   else     puts 'Result is: #{read_array[0].read_nonblock(30)}'    end    prompt   end 

    It’s probably not as elegant as you might like, but it definitely gets the job done without mucking around with threads. Unfortunately, this won’t help you should you want something more robust (timer/sending a signal to the process), and sadly, I have no clue if this works in JRuby. Would love to know if it does though 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 249k
  • Answers 249k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you would like a link in one frame to… May 13, 2026 at 9:03 am
  • Editorial Team
    Editorial Team added an answer I'd recommend Shuber's Encryptor - it wraps the OpenSSL library… May 13, 2026 at 9:03 am
  • Editorial Team
    Editorial Team added an answer How would you expect recode to know that a file… May 13, 2026 at 9:03 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.