Created
June 1, 2012 16:25
-
-
Save eam/2853343 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'rubygems' | |
require 'eventmachine' | |
$done = false | |
module FdWatcher | |
def notify_readable | |
puts "readable event" | |
begin | |
buf = @io.sysread(4096) | |
puts "saw #{buf.length} bytes of data: '#{buf}'" | |
rescue SystemCallError, EOFError => ex | |
puts "DEBUG: saw read exception #{ex}" | |
detach | |
$done = true | |
rescue => ex | |
puts "else #{ex.inspect}" | |
end | |
end | |
end | |
class FdClass < EventMachine::Connection | |
include FdWatcher | |
end | |
puts "hello world" | |
def add_subprocess(cmd) | |
stdin_rd, stdin_wr = IO.pipe | |
stdout_rd, stdout_wr = IO.pipe | |
stderr_rd, stderr_wr = IO.pipe | |
pid = fork | |
if not pid.nil? | |
# parent | |
#EM.watch(stdin_wr, FdClass) { |c| c.notify_writable = true } | |
EM.watch(stdout_rd, FdClass) { |c| c.notify_readable = true } | |
else | |
STDIN.reopen(stdin_rd) | |
STDOUT.reopen(stdout_wr) | |
STDERR.reopen(stderr_wr) | |
exec(*cmd) | |
end | |
end | |
EM.run { | |
EM.schedule { | |
puts "hello scheduled, starting up" | |
add_subprocess(%w{ /bin/echo -n hello world child process }) | |
} | |
EM.add_periodic_timer(0.5) { | |
puts "TIMER! done: #{$done}" | |
if $done | |
EM.stop_event_loop | |
end | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment