I'm looking for a fast way to "reset" Ruby. That is, to reset the
I've got code for this kind of testrunner; no specific optimizations,
though. It's a plain testrunner using pipes for communication and a
fork to separate out the execution. There's something clumsy about it
- I don't remember what it was.
Well, there's exit! in the code below that I find rather clumsy, but I don't
think that is what you mean. Other than that, would your code look similar?
(code below is an experiment that I put in webrick. yes, I found webricks
cgihandler and it works fine; in theory this should be faster, in practise
the ruby-cgi program that was started was too slow to notice the difference,
which -incidentally- was caused by CGI.new("html4") taking lots of time;
enough of that, let's look at the code).
mm, I forgot to reopen stderr, I see now.
# Run +program+ giving it +input+ over stdin and producing its output as a
# String. The optional +max_time+ is used as a timeout. It is assumed that the
# environment (Ruby's ENV) has been prepared.
def CGI.runner(program, input="", max_time=1.0)
# rd, wr = IO.pipe
parent2child = IO.pipe
child2parent = IO.pipe
if fork
result = ""
parent2child[0].close
child2parent[1].close
parent2child[1].print input
while line = child2parent[0].gets
result << line
end
child2parent[0].close
parent2child[1].close
result
else
begin
parent2child[1].close
child2parent[0].close
$stdin.reopen parent2child[0]
$stdout.reopen child2parent[1]
timeout(max_time) {
load program
} # rescue TimeoutError
child2parent[1].close
parent2child[0].close
ensure
# Do not interfer with the 'real' server stuff (of the parent), whose
# code is running here as well, specifically including closing of the SSL
# socket.
exit!
end
end
end