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_status, #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_sock_opt, #get_sockname, #get_status, new, #notify_readable=, #notify_readable?, #notify_writable=, #notify_writable?, #pause, #paused?, #pending_connect_timeout, #pending_connect_timeout=, #post_init, #proxy_incoming_to, #proxy_target_unbound, #reconnect, #resume, #send_data, #send_datagram, #send_file_data, #set_comm_inactivity_timeout, #set_pending_connect_timeout, #ssl_handshake_completed, #ssl_verify_peer, #start_tls, #stop_proxying, #stream_file_data

Constructor Details

#initializeDeferrableChildProcess

:nodoc:



42
43
44
45
# File 'lib/em/processes.rb', line 42

def initialize # :nodoc:
  super
  @data = []
end

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.



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

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

Instance Method Details

#receive_data(data) ⇒ Object

:nodoc:



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

def receive_data data # :nodoc:
  @data << data
end

#unbindObject

:nodoc:



67
68
69
# File 'lib/em/processes.rb', line 67

def unbind # :nodoc:
  succeed( @data.join )
end