Skip to content

Instantly share code, notes, and snippets.

@vpereira
Created August 12, 2011 12:13

Revisions

  1. vpereira created this gist Aug 12, 2011.
    28 changes: 28 additions & 0 deletions em_pipe_reader.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    require 'fcntl'
    require 'eventmachine'

    #what is needed
    #mkfifo mypipe
    #that everything written thru the pipe have a "\n"
    #ex: echo "foobar\n" > mypipe

    #HPO = Handle Pipe Output
    module HPO
    def notify_readable
    puts @io.readlines
    end
    def unbind
    EM.next_tick do
    data = @io.read
    puts dat
    end
    end
    end

    EM.run {
    fd = IO.sysopen('mypipe',Fcntl::O_RDONLY|Fcntl::O_NONBLOCK)
    #i think i dont have to repeat the open mode but egal
    myIO = IO.new(fd,Fcntl::O_RDONLY|Fcntl::O_NONBLOCK)
    fdmon = EM.watch myIO, HPO
    fdmon.notify_readable = true
    }