Professional Web Applications Themes

io/nonblock - blocks w/threads? - Ruby

this program does not work as expected w/o inserting the 'Thread.critical' bits - i must say i do not understand why, can someone help me understand? require 'tk' require 'open3' require 'io/nonblock' $VERBOSE=nil r=TkRoot.new l=TkLabel.new r, :text=>`ruby -e "p Time.now"` l.pack command='ruby -e "loop{p Time.now; sleep 1}"' i,o,e = Open3::popen3 command i.close text = nil Thread.new do loop do rios, = select [o,e], nil, nil rios.map do |rio| next if rio.eof? Thread.critical = true # blocks w/o this rio.nonblock{ text = rio.read } Thread.critical = false # blocks w/o this l.configure :text=>text end end end Tk.mainloop is this an o.k. ...

  1. #1

    Default io/nonblock - blocks w/threads?


    this program does not work as expected w/o inserting the 'Thread.critical'
    bits - i must say i do not understand why, can someone help me understand?

    require 'tk'
    require 'open3'
    require 'io/nonblock'
    $VERBOSE=nil

    r=TkRoot.new
    l=TkLabel.new r, :text=>`ruby -e "p Time.now"`
    l.pack

    command='ruby -e "loop{p Time.now; sleep 1}"'
    i,o,e = Open3::popen3 command
    i.close
    text = nil

    Thread.new do
    loop do
    rios, = select [o,e], nil, nil
    rios.map do |rio|
    next if rio.eof?
    Thread.critical = true # blocks w/o this
    rio.nonblock{ text = rio.read }
    Thread.critical = false # blocks w/o this
    l.configure :text=>text
    end
    end
    end

    Tk.mainloop


    is this an o.k. technique to prevent a non-blocking read not to hand a
    multi-threaded ap? i'm think of adding an asychronous processing option to my
    session library using this feature... actually i suppose it'd always be o.k.
    for this to be there...

    -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: io/nonblock - blocks w/threads?


    "Ara.T.Howard" <gov> schrieb im Newsbeitrag
    news:ngdc.noaa.gov... 
    'Thread.critical' 
    understand? 

    Why do you use map here? Apparently you don't access the result of
    mapping so each would be more appropriate.
     

    I'm not really sure I understand correctly what you're after. Could you
    elaborate that?

    Generally speaking there's one piece of advice:

    When using Thread.critical= it is always best to do reset the flag in an
    ensure clause to make sure that the reset occurs under all conditions:

    Thread.critical = true
    begin
    # do stuff
    ensure
    Thread.critical = false
    end
     
    o.k. 

    Regards

    robert

    Robert Guest

  3. #3

    Default Re: io/nonblock - blocks w/threads?

    On Wed, 25 Feb 2004, Robert Klemme wrote:
     
    > 'Thread.critical' 
    > understand? 
    >
    > Why do you use map here? Apparently you don't access the result of
    > mapping so each would be more appropriate.[/ref]

    right you are - in the actual code i do - this is a distilled example...
     
    >[/ref]
     

    a) why is the critical section needed to prevent one thread blocking the
    entire process?

    b) is this approach (critical section) the correct way of dealing with this
    issue?

    more fundmenetally WHY does using 'nonblock/read' block a thread when other io
    ops, 'gets' for example do not? WHAT is the relationship between ioctl ops
    and Thread.critical?
     

    yes of course - i've always wondered why that one doesn't take a block...

    -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

  4. #4

    Default Re: io/nonblock - blocks w/threads?


    Some answers - but not all...

    "Ara.T.Howard" <ngdc.noaa.gov> schrieb im Newsbeitrag
    news:ngdc.noaa.gov... 
    > > 'Thread.critical' 
    > > understand? 
    > >
    > > Why do you use map here? Apparently you don't access the result of
    > > mapping so each would be more appropriate.[/ref]
    >
    > right you are - in the actual code i do - this is a distilled example...[/ref]

    Ah!
     [/ref][/ref]

    > >[/ref]
    > [/ref]
    you 
    >
    > a) why is the critical section needed to prevent one thread blocking[/ref]
    the 

    That's an interesting question. One would rather expect
    "Thread.critical=true" to block the whole process. I guess, that there
    are no thread context changes without these methods and assigning to
    Thread.critical has the side effect of doing a context switch if possible.
    I'd try to use "Thread.pass" after "l.configure..." instead of
    "Thread.critical" to give other threads a chance to run and see what
    happens.
     
    this 

    If the blocking is indeed caused by the loop spinning endlessly,
    Thread.pass is a far better alternative. You could as well use sleep to
    achieve the same.
     
    other io 
    ops 

    That I don't know, maybe Matz or Nobu can comment on that.
     [/ref]
    an 
    >
    > yes of course - i've always wondered why that one doesn't take a[/ref]
    block...

    I guess because normally it's not intended for use in the open range.
    Normally one would use higher level constructs such as Mutex, Queue etc.

    Regards

    robert


    Robert Guest

  5. #5

    Default Re: io/nonblock - blocks w/threads?


    Robert Klemme said: 
    > block...
    >
    > I guess because normally it's not intended for use in the open range.
    > Normally one would use higher level constructs such as Mutex, Queue etc.[/ref]

    require 'thread'

    Thread.exclusive{
    // Critical Code Here
    }

    --
    -- Jim Weirich org http://onestepback.org
    -----------------------------------------------------------------
    "Beware of bugs in the above code; I have only proved it correct,
    not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)



    Jim Guest

  6. #6

    Default Re: io/nonblock - blocks w/threads?

    On Wed, 25 Feb 2004, Robert Klemme wrote:
     

    thanks for the response...
     
    > this 
    >
    > If the blocking is indeed caused by the loop spinning endlessly, Thread.pass
    > is a far better alternative. You could as well use sleep to achieve the
    > same.[/ref]

    it's the actual call to nonblock that blocks the process, try this using the
    two different approaches between the print statements:

    require 'tk'
    require 'open3'
    require 'io/nonblock'
    require 'thread'
    $VERBOSE=nil
    STDOUT.sync = STDERR.sync = true

    r=TkRoot.new
    l=TkLabel.new r, :text=>`ruby -e "p Time.now"`
    l.pack

    command='ruby -e "loop{p Time.now; sleep 1}"'
    i,o,e = Open3::popen3 command
    i.close
    text = nil

    Thread.new do
    loop do
    rios, = select [o, e], nil, nil
    rios.map do |rio|
    next if rio.eof?
    p 'start...'
    rio.nonblock{ text = rio.read } # blocks
    #text = rio.gets # does not block
    p 'finish'
    l.configure :text=>text
    end
    end
    end

    Tk.mainloop


    i also tried a couple of ways on inserting Thread.pass - outside of nonblock's
    block, inside of it, etc. - nothing works except protecting the call to read
    by critical/exclusive which does really make sense since the point of nonblock
    is not to?


    -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

  7. #7

    Default Re: io/nonblock - blocks w/threads?

    On Thu, 26 Feb 2004, Jim Weirich wrote:
     
    > > block...
    > >
    > > I guess because normally it's not intended for use in the open range.
    > > Normally one would use higher level constructs such as Mutex, Queue etc.[/ref]
    >
    > require 'thread'
    >
    > Thread.exclusive{
    > // Critical Code Here
    > }[/ref]

    rtfm eh? ;-)

    -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

Similar Threads

  1. #39068 [NEW]: 8Kb write buffer on nonblock sockets
    By dmitrysp at yandex dot ru in forum PHP Bugs
    Replies: 1
    Last Post: October 6th, 06:05 PM
  2. color blocks
    By moxx123 in forum Macromedia Freehand
    Replies: 3
    Last Post: April 7th, 04:19 AM
  3. io/nonblock
    By Ara.T.Howard in forum Ruby
    Replies: 4
    Last Post: February 6th, 03:03 PM
  4. question about blocks
    By Ferenc in forum Ruby
    Replies: 7
    Last Post: January 21st, 09:01 PM
  5. Do BEGIN blocks and END blocks have priority?
    By Dan Anderson in forum PERL Beginners
    Replies: 8
    Last Post: September 29th, 02:21 AM

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