Sending FCGI requests *from* Ruby

Writing FCGI apps in Ruby, which are spawned from Apache or lighttpd, is
straightforward enough.

What I want to know is, what’s the best way which a Ruby program can
other FCGI apps and send requests to them? And has anyone had success
doing this?

The application I have in mind is to make use of some existing perl
code. I
don’t want to do

rc = system(“/usr/bin/perl”, “”, *args)

for every operation, as this spawns a whole perl interpreter each time.

So what I wanted to do was to make minimal changes to the perl code so
it could accept multiple requests in a loop.

I could just popen() each perl script and chat down stdin/stdout, but
limits me to one instance of this application per Ruby process. In
there will be 10 or 20 different perl scripts that I want to persist,
each Ruby process will only invoke one or two of them before it

However, if I use FCGI, I can have shared pools of Perl instances to be
invoked by other Ruby processes as they come and go, and the size of
pool can be tuned statically or dynamically. This is very attractive.

So how to achieve this? I could run those perl FCGI’s under Apache, and
use Ruby’s Net::HTTP to send requests to Apache which in turn sends them
those FCGIs. But I wondered if there was a more lightweight way to do
formatting the FCGI requests and/or starting the pool of FCGI slave
processes directly from Ruby.

(Having said that, using HTTP as the glue, optionally with XMLRPC or
running over it, would arguably be a “standard” solution)

I can use the standalone program cgi-fcgi to start the pool members:

So what I’m really looking for is something like the cgi-fcgi’s “-bind
-connect” option, to pass a CGI request down the socket, but written in
or wrapped for Ruby. But I’m also open to suggestions for other ways to
achieve what I’m trying to do.



P.S. Searching around, I did notice there’s a webrick-fcgi module, which
thought maybe I could rip some code from.

The website pointed to by

This returns Content-Type: application/octet-stream, meaning the page
not display in a browser!

Furthermore, if you try to download the source from
you get an HTML page rather than a tarball. So I can’t download it, and
I imagine it’s unlikely that many people are using it.

$ telnet 80
Connected to
Escape character is ‘^]’.
GET /projects/ruby/webrick-fcgi/ HTTP/1.0

HTTP/1.0 200 OK
Connection: close
Content-Type: application/octet-stream <<<<< !
ETag: “2004059686”
Accept-Ranges: bytes
Last-Modified: Sat, 11 Jun 2005 22:35:45 GMT
Content-Length: 582
Date: Thu, 01 Feb 2007 12:01:21 GMT
Server: lighttpd/1.4.13




webrick-fcgi is a WEBrick-compatible API for the FCGI library.


Version 0.1.0: webrick-fcgi-0.1.0.tar.gz


Version 0.1.0: rdoc

Connection closed by foreign host.