Professional Web Applications Themes

timeout w/o timeout.rb - Ruby

i'm writing some unit tests which do alot of io/select/etc - bad things to mix with a thread based 'timeout' method... i've been using this instead: # a timeout method which does not fail when io blocks trap 'SIGALRM', 'IGNORE' class TimeoutError < StandardError; end def timeout n begin trap('SIGALRM'){throw :timeout, TimeoutError.new} cid = nil unless((cid = fork)) sleep n Process.kill 'SIGALRM', Process.ppid else thrown = catch(:timeout) do ret = yield Process.kill 'SIGKILL', cid ret end raise thrown if TimeoutError === thrown end ensure trap 'SIGALRM', 'IGNORE' end end any massive problems with this (this impl is a mega hack ...

  1. #1

    Default timeout w/o timeout.rb



    i'm writing some unit tests which do alot of io/select/etc - bad things to mix
    with a thread based 'timeout' method... i've been using this instead:

    # a timeout method which does not fail when io blocks
    trap 'SIGALRM', 'IGNORE'
    class TimeoutError < StandardError; end
    def timeout n
    begin
    trap('SIGALRM'){throw :timeout, TimeoutError.new}
    cid = nil
    unless((cid = fork))
    sleep n
    Process.kill 'SIGALRM', Process.ppid
    else
    thrown =
    catch(:timeout) do
    ret = yield
    Process.kill 'SIGKILL', cid
    ret
    end
    raise thrown if TimeoutError === thrown
    end
    ensure
    trap 'SIGALRM', 'IGNORE'
    end
    end

    any massive problems with this (this impl is a mega hack i realize)?

    does it seem like some permutation of this might be useful? something like
    Process#timeout perhaps??

    -a
    --

    ATTN: please update your address books with address below!

    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | STP :: http://www.ngdc.noaa.gov/stp/
    | NGDC :: http://www.ngdc.noaa.gov/
    | NESDIS :: http://www.nesdis.noaa.gov/
    | NOAA :: http://www.noaa.gov/
    | US DOC :: http://www.commerce.gov/
    |
    | The difference between art and science is that science is what we
    | understand well enough to explain to a computer.
    | Art is everything else.
    | -- Donald Knuth, "Discover"
    |
    | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
    ================================================== =============================

    Ara.T.Howard Guest

  2. #2

    Default Re: timeout w/o timeout.rb

    Ara.T.Howard wrote: 

    Doesn't work on windows (this is a problem with me, not with your code :-) )

    Regards,
    Pit


    Pit Guest

  3. #3

    Default Re: timeout w/o timeout.rb

    Ara.T.Howard wrote:
     

    Doesn't Ruby use SIGALRM internally? I could be wrong, but if it does,
    you may be asking for trouble by using it in your script.

    - Jamis

    --
    Jamis Buck
    byu.edu
    http://www.jamisbuck.org/blog/jamis.cgi

    ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'



    Jamis Guest

  4. #4

    Default Re: timeout w/o timeout.rb

    On Tue, 10 Feb 2004, Jamis Buck wrote:
     
    >
    > Doesn't Ruby use SIGALRM internally? I could be wrong, but if it does,
    > you may be asking for trouble by using it in your script.
    >
    > - Jamis[/ref]

    ah - good point - and SIGUSR1 would work just as well

    -a
    --

    ATTN: please update your address books with address below!

    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | STP :: http://www.ngdc.noaa.gov/stp/
    | NGDC :: http://www.ngdc.noaa.gov/
    | NESDIS :: http://www.nesdis.noaa.gov/
    | NOAA :: http://www.noaa.gov/
    | US DOC :: http://www.commerce.gov/
    |
    | The difference between art and science is that science is what we
    | understand well enough to explain to a computer.
    | Art is everything else.
    | -- Donald Knuth, "Discover"
    |
    | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
    ================================================== =============================

    Ara.T.Howard Guest

Similar Threads

  1. #40750 [NEW]: fsockopen timeout parameter overwrites timeout for reading
    By andreas dot rieber at 2e-systems dot com in forum PHP Bugs
    Replies: 14
    Last Post: March 14th, 07:22 PM
  2. cfquery, timeout attribute and cfserver timeout setting
    By Telix in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: July 1st, 03:42 PM
  3. Replies: 3
    Last Post: April 29th, 02:21 AM
  4. Timeout sometimes runs past the timeout length
    By Stuart in forum Macromedia Director Lingo
    Replies: 1
    Last Post: August 20th, 04:28 PM
  5. Replies: 0
    Last Post: August 7th, 12:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139