forked from travis-ci/travis-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve EM::Stdout by using EM.attach
- Loading branch information
Sven Fuchs
committed
Jan 6, 2011
1 parent
8bb2475
commit a95b695
Showing
9 changed files
with
92 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'rubygems' | ||
require 'em/stdout' | ||
|
||
EventMachine.module_eval do | ||
def self.split_stdout(&block) | ||
stdout = nil | ||
EM.next_tick do | ||
read, write = IO.pipe | ||
stdout = STDOUT.clone | ||
EM.attach(read, Splitter) { |c| | ||
c.stdout = stdout | ||
c.callback = block | ||
} | ||
STDOUT.reopen(write) | ||
end | ||
sleep(0.01) until stdout | ||
stdout | ||
end | ||
|
||
class Splitter < EventMachine::Connection | ||
attr_accessor :stdout, :callback | ||
|
||
def receive_data(data) | ||
callback.call(data) | ||
end | ||
|
||
def unbind | ||
STDOUT.reopen(stdout) | ||
stdout.puts "unbound" | ||
end | ||
end | ||
end | ||
|
||
|
||
STDOUT.sync = true | ||
|
||
EM.run do | ||
EM.defer do | ||
stdout = EM.split_stdout do |data| | ||
stdout.puts '--: ' + data.inspect | ||
sleep(1) | ||
end | ||
|
||
stdout.puts 'starting' | ||
|
||
10.times { puts 'output ... '; sleep(0.2) } | ||
|
||
stdout.puts 'stopping' | ||
EM.stop | ||
end | ||
end | ||
puts 'fin' |
This file contains 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
This file contains 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