Professional Web Applications Themes

threads and blocking - Ruby

under which conditions will a blocking operation in a thread block the entire process? i thought i understood this but was suprised that ~ > ruby -e 'Thread.new{gets}; Thread.new{select nil}; Thread.new{puts 42}' 42 does not seem to block the entire process. i also was suprised that this worked: ~ > printf "4\n2\n" | ruby -e '[Thread.new{p gets.chop},Thread.new{p gets.chop}].map{|t| t.join}' "4" "2" i have been in situations before where one thread blocking has blocked my entire application and remember thinking to myself - "don't do blocking i/o from threads". but that appears to be too strong of a statement. can someone ...

  1. #1

    Default threads and blocking



    under which conditions will a blocking operation in a thread block the entire
    process?

    i thought i understood this but was suprised that

    ~ > ruby -e 'Thread.new{gets}; Thread.new{select nil}; Thread.new{puts 42}'
    42

    does not seem to block the entire process.

    i also was suprised that this worked:

    ~ > printf "4\n2\n" | ruby -e '[Thread.new{p gets.chop},Thread.new{p
    gets.chop}].map{|t| t.join}'
    "4"
    "2"

    i have been in situations before where one thread blocking has blocked my
    entire application and remember thinking to myself - "don't do blocking i/o
    from threads". but that appears to be too strong of a statement.

    can someone concisely enumerate the things to avoid?

    regards.

    -a
    --
    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ================================================== =============================

    Ara.T.Howard Guest

  2. #2

    Default Re: threads and blocking

    Shashank Date wrote: 

    Doesn't block in ruby 1.9.0 (2004-02-15) [i686-linux]. Maybe a windows
    effect? My (limited) understanding of ruby threads is that thread
    waiting on IO will not block the app.

    Also, a thread calling system will not block the app, as in

    Thread.new {system "sleep 10"}

    However, flock does block the process.



    Joel Guest

  3. #3

    Default Re: threads and blocking

    Joel VanderWerf wrote:
     
    >
    >
    > Doesn't block in ruby 1.9.0 (2004-02-15) [i686-linux]. Maybe a windows
    > effect? My (limited) understanding of ruby threads is that thread
    > waiting on IO will not block the app.
    >
    > Also, a thread calling system will not block the app, as in
    >
    > Thread.new {system "sleep 10"}
    >
    > However, flock does block the process.
    >
    >[/ref]
    On windows sockets will not generally block Ruby, but if you try a
    TCP#connect on a non-existant IP address, then all of Ruby will block
    for about 20 seconds. I think that this is a result of a poor windows
    non-blocking socket implementation. One solution that I have thought of
    but not tried, would be to use blocking sockets on windows and do the
    operation within a native thread and then just signal back to the ruby
    thread when there is a result of the operation.

    Steve Tuckner



    Steve Guest

  4. #4

    Default Re: threads and blocking


    "Steve Tuckner" <com> schrieb im Newsbeitrag
    news:com... 
    > >
    > >
    > > Doesn't block in ruby 1.9.0 (2004-02-15) [i686-linux]. Maybe a windows
    > > effect? My (limited) understanding of ruby threads is that thread
    > > waiting on IO will not block the app.
    > >
    > > Also, a thread calling system will not block the app, as in
    > >
    > > Thread.new {system "sleep 10"}
    > >
    > > However, flock does block the process.
    > >
    > >[/ref]
    > On windows sockets will not generally block Ruby, but if you try a
    > TCP#connect on a non-existant IP address, then all of Ruby will block
    > for about 20 seconds. I think that this is a result of a poor windows
    > non-blocking socket implementation.[/ref]

    I'd guess it is due to differences in DNS-resolution. Maybe there are
    different timeouts effective on Windows and Linux / Solaris etc.
     

    Akward. But such things happens all too often in Windows Land... :-(

    robert

    Robert Guest

Similar Threads

  1. Blocking on multiple threads with timeout
    By Jonathon McKitrick in forum FreeBSD
    Replies: 2
    Last Post: March 1st, 08:53 PM
  2. non-blocking io
    By Ara.T.Howard in forum Ruby
    Replies: 2
    Last Post: December 12th, 07:04 PM
  3. blocking i/o vs. non-blocking i/o
    By Andre in forum UNIX Programming
    Replies: 4
    Last Post: October 11th, 08:15 PM
  4. Will Thread blocking in read() leads to process blocking?
    By Loic Domaigne in forum UNIX Programming
    Replies: 4
    Last Post: July 23rd, 12:36 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