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.

Constant Summary

Constants included from Deferrable

EventMachine::Deferrable::Pool

Instance Attribute Summary

Attributes inherited from Connection

#signature

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deferrable

#callback, #cancel_callback, #cancel_errback, #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_cipher_bits, #get_cipher_name, #get_cipher_protocol, #get_idle_time, #get_outbound_data_size, #get_peer_cert, #get_peername, #get_pid, #get_proxied_bytes, #get_sni_hostname, #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_completed, #proxy_incoming_to, #proxy_target_unbound, #reconnect, #resume, #send_data, #send_datagram, #send_file_data, #set_sock_opt, #ssl_handshake_completed, #ssl_verify_peer, #start_tls, #stop_proxying, #stream_file_data

Constructor Details

#initializeDeferrableChildProcess

Returns a new instance of DeferrableChildProcess.



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

def initialize
  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.



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

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

Instance Method Details

#receive_data(data) ⇒ Object



65
66
67
# File 'lib/em/processes.rb', line 65

def receive_data data
  @data << data
end

#unbindObject



70
71
72
# File 'lib/em/processes.rb', line 70

def unbind
  succeed( @data.join )
end