Ticket #89 (new enhancement)

Opened 2 weeks ago

Last modified 2 weeks ago

EM.system wrapper for EM.popen (async verison of system())

Reported by: tmm1 Assigned to: raggi
Priority: minor Milestone: EventMachine "Framework"
Keywords: Cc:

Description

Async version of Kernel.system using EM.popen:

require 'rubygems'
require 'eventmachine'

module EventMachine
  module SystemCmd
    def initialize
      @output = []
    end
    def receive_data data
      @output << data
    end
    def unbind
      @cb.call(@output.join('')) if @cb
    end
  end

  def EM.system cmd, &cb
    EM.popen cmd, SystemCmd do |c|
      c.instance_variable_set('@cb', cb)
    end
  end
end

Can be used as follows:

EM.run{
  EM.system('ls /') do |results|
    puts results
  end
}

Change History

10/13/08 16:35:28 changed by tmm1

To handle system('ls', '/') style syntax:

  def EM.system *cmd, &cb
    EM.popen cmd.join(' '), SystemCmd do |c|
      c.instance_variable_set('@cb', cb)
    end
  end

10/13/08 16:41:31 changed by tmm1

Return the exit status to the callback as well:

module EventMachine
  module SystemCmd
    def initialize
      @output = []
    end
    def receive_data data
      @output << data
    end
    def unbind
      @cb.call @output.join(''), get_status if @cb
    end
  end

  def EM.system *cmd, &cb
    EM.popen cmd.join(' '), SystemCmd do |c|
      c.instance_variable_set('@cb', cb)
    end
  end
end

EM.run{
  EM.system('ls', '/') do |output, status|
    puts output
    puts status
    EM.stop
  end 
}