Class: EventMachine::DeferrableChildProcess

Inherits:
Connection
  • Object
show all
Includes:
Deferrable
Defined in:
lib/em/processes.rb

Overview

EM::DeferrableChildProcess is a sugaring of a common use-case involving EM::popen. Call the #open method on EM::DeferrableChildProcess, passing a command-string. #open immediately returns an EM::Deferrable object. It also schedules the forking of a child process, which will execute the command passed to #open. When the forked child terminates, the Deferrable will be signalled and execute its callbacks, passing the data that the child process wrote to stdout.

Instance Attribute Summary

Attributes inherited from Connection

#signature

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deferrable

#callback, #cancel_timeout, #errback, #fail, future, #set_deferred_failure, #set_deferred_status, #set_deferred_success, #succeed, #timeout

Methods inherited from Connection

#associate_callback_target, #close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #connection_completed, #detach, #error?, #get_outbound_data_size, #get_peer_cert, #get_peername, #get_pid, #get_sockname, #get_status, #initialize, new, #post_init, #reconnect, #send_data, #send_datagram, #send_file_data, #set_comm_inactivity_timeout, #ssl_handshake_completed, #start_tls, #stream_file_data

Constructor Details

This class inherits a constructor from EventMachine::Connection

Class Method Details

.open(cmd) ⇒ Object

Sugars a common use-case involving forked child processes. #open takes a String argument containing an shell command string (including arguments if desired). #open immediately returns an EventMachine::Deferrable object, without blocking.

It also invokes EventMachine#popen to run the passed-in command in a forked child process.

When the forked child terminates, the Deferrable that #open calls its callbacks, passing the data returned from the child process.



54
55
56
# File 'lib/em/processes.rb', line 54

def self.open cmd
	EventMachine.popen( cmd, DeferrableChildProcess )
end

Instance Method Details

#receive_data(data) ⇒ Object



58
59
60
# File 'lib/em/processes.rb', line 58

def receive_data data
	(@data ||= []) << data
end

#unbindObject



62
63
64
# File 'lib/em/processes.rb', line 62

def unbind
	succeed( @data.join )
end