Forum: Ruby Sending FCGI requests *from* Ruby

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-02-01 13:21
(Received via mailing list)
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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                <link rel='Stylesheet' href='../../site.css'
type='text/css' />        </head>
                <p>webrick-fcgi is a WEBrick-compatible API for the FCGI
                <p>Version 0.1.0: <a
                <p>Version 0.1.0: <a
href='webrick-fcgi-0.1.0/doc'>rdoc</a></p>        </body>
Connection closed by foreign host.
This topic is locked and can not be replied to.