Created
May 8, 2012 11:47
-
-
Save hawx/2634419 to your computer and use it in GitHub Desktop.
guard-shell with forking?
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
module Processes | |
extend self | |
def max | |
@max || 10 | |
end | |
attr_writer :max | |
def processes | |
@processes ||= [] | |
end | |
def can_fork_process? | |
Process.respond_to?(:fork) && max > 0 | |
end | |
def fork(command) | |
if processes.size < max | |
processes << Process.fork { Process.exec *command } | |
else | |
process.delete Process.wait | |
fork command | |
end | |
end | |
def run(command) | |
if can_fork_process? | |
fork command | |
else | |
exec command | |
end | |
end | |
end | |
Processes.max = 5 | |
guard :shell do | |
watch /.*/ do |m| | |
Processes.run "say #{m[0]}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment