2

How do you build your own FastCGI interface in PHP?

I'd like to do something similar to what they're doing in Perl, but in PHP. Is it even possible? Would it be faster?

(That is, I'd like to be able to load a web app framework once into memory, and then just have FastCGI call a method I provide for every request. So not the more generic preloading of the PHP-interpreter that is happening in the "default" PHP FastCGI setup.)

cheers!

(Edit: Isn't Mongrel and RoR doing this as well?)

Ok I've made a freakin' diagram now :)

alt text

Community
  • 1
  • 1
0scar
  • 3,200
  • 25
  • 28
  • Yes, Mongrel & RoR are doing this. RoR creates the MVC application, and Mongrel calls the requisite methods. However, I've no clue how to work with FastCGI, so I can't tell you how to imitate it. – Robert K Jun 01 '09 at 15:41

3 Answers3

1

I may be mistaken (it's late) but aren't you just trying to do some form of caching? Regardless, the FastCGI interface seems to be fairly well defined. So, it should be possible to do whatever you want, fairly easily.

sybreon
  • 3,128
  • 18
  • 19
  • Well, it could be that caching is the way to go with PHP. What I'm really looking for is to have PHP code loaded _and_ initiliazed in memory. Imagine a framework with a lot of objects. As it's now it seems to me that all code needs to be initialized for every request. (Even if this process may be sped up by caching the bytecode etc.) I'd like to be able to write a framwork that has the ability to setup everything once. I guess :) This might no make sense in the face of clever PHP caching mechanisms. I don't know. – 0scar Jun 01 '09 at 15:10
  • But wouldn't this take up much more memory, which would also slow things down? – sybreon Jun 02 '09 at 01:50
  • Well in practice yes, since the heap would probably grow larger and larger with every request. But theoretically, no. You're loading the same things. You just do it before a request, once, instead of on every request. – 0scar Jun 02 '09 at 11:59
  • "FastCGI interface seems to be fairly well defined. So, it should be possible to do whatever you want, fairly easily" .. great! So how do you do it? – 0scar Jun 02 '09 at 12:01
  • Since it's based of CGI I guess it's something like this. You need to provide the PHP script with some environment variables, and then read the PHP output off stdin. Then, do your magic and pass it on to the web-server. – sybreon Jun 02 '09 at 15:39
0

You can not do it in PHP for PHP, you can do it in C for cgi-sapi but you probably want to use APC instead.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • Are you absolutely sure of this? Why not? – 0scar Jul 15 '09 at 10:58
  • My bad, i meant fastcgi sapi actually. @Oscar: Are you asking why doesn't it do what you expect? Check out http://php.net/apc it solves your needs. – jpic Jul 16 '09 at 12:30
0

If you are really determined for it to be in PHP, you could in theory, create a php module in c, and form the bridge for fastCGI in c.

After which im guessing, you would run a PHP script accessing fastCGI as a standalone program, running a loop and accepting requests. In which your using PHP as a "persistent virtual machine" (could not find a better term), that communicates via fastCGI.

I do agree that the reloading of several of your dependency libaries / etc, is a very easily over-sighted performance drain. However, it maybe intentional design, in simplifying the PHP development cycle. Think of the global variable mess inexperienced users would make, in a persistent environment. Toss in the fact that most fastCGI setup, will initialize multiple copies / threads according to the load. Its a small can of worms. Starting a blank new slate for each request simplifies it.

However, honestly... if your that concern in performance, you might as well consider fastCGI C++ / node.js, as alternatives.... Having developed for JavaScript/PHP/C++, your idea is definitely possible... However it really boils down to cost-benefit... Is it worth the effort?, etc.

PS : You can also consider compiling your PHP sites via hip-hop.

Community
  • 1
  • 1
PicoCreator
  • 9,886
  • 7
  • 43
  • 64