windoze types-
i have a ruby module, spawn (like popen3 w/o a fork), that relies on a system
call to ‘mkfifo’. currenlty i’m just using a system call because just about
any system with for should also have a ‘mkfifo’ call. i’m considering making
this a c extension but am unsure if windows has a posix compliant mkfifo call?
i realize this is slightly off topic but the spawn method is really useful and
seems to be quite a bit faster than popen3 as well.
thanks
-a
btw. here is the ‘baby’ code for Spawn:
#!/usr/bin/env ruby
require ‘open3’
require ‘tmpdir’
require ‘io/wait’
$VERBOSE=nil
module Spawn
class << self
def spawn cmd
ipath, opath, epath = tmpfifo, tmpfifo, tmpfifo
system “#{ cmd } < #{ ipath } 1> #{ opath } 2> #{ epath } &”
[open(ipath, ‘w’), open(opath, ‘r’), open(epath, ‘r’)]
end
def tmpfifo
path = nil
42.times do |i|
tpath = File.join(Dir.tmpdir, “#{ $$ }.#{ rand }.#{ i }”)
system “mkfifo #{ tpath }” # how to do this in windows, C or shell?
next unless $? == 0
path = tpath
at_exit{ File.unlink(path) rescue STDERR.puts(“rm <#{ path }> failed”) }
break
end
raise “could not generate tmpfifo” unless path
path
end
end
end
if $0 == FILE
mode = ARGV.shift || ‘open3’
program = <<-stmts
echo 42
sleep 1
echo 42
sleep 1
echo done # signals end of stdout
echo done 1>&2 # signals end of stderr
stmts
done = %r/^\s*done\s*$/o
thread =
Thread.new do
i,o,e = (mode =~ /open3/io ? Open3::popen3(‘sh’) : Spawn::spawn(‘sh’))
Thread.new do
Thread.new do
program.each{|stmt| i.puts stmt}
i.flush
end.join
end
rfds = [o,e]
loop do
break if rfds.empty?
rs, = select rfds, nil, nil
rs.each do |r|
rfds.delete(r) and next if r.eof?
line = ''
line << r.getc while r.ready?
rfds.delete(r) and line[done]='' if line =~ done
next if line.empty?
printf "%s @ %s: %s\n", (r == o ? 'o' : 'e'), Time.now.to_f, line.inspect
end
end
end
thread.join
end
-a
···
–
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================