Friday, March 20, 2009

PHP fork() 'fake' with apache

The other day at work I started working on a php script to dynamically return some results to a command line client(wget/curl)... The problem was, the php script takes quite a while to get the db results, find a set of images, and then compress them into a zip file, before streaming them to the client. I knew that my jobs where perfect for parallel processing as one set of data is selected by the file timestamps and the other comes out of the database.

Being familiar with C and linux cl programming, I naturally turned to the PHP fork() function. However, I soon found out that fork() in php one works if PHP is running in CGI or CLI mode. This code needed to run through Apache!

Since each PHP script gets it's own process from Apache, I figured that if I where to use some clever manimuplating of output bufferes and file pipes, I should be able to make this work!

So here is the prototype fork with apache: phpApachFork.php

Fixed -- missing function sendToHost()
Fixed -- sendToHost not properly handling socket closing after content length reached.

1 comment:

  1. AWHAWHAW, this happens to me all the time! (This is me pretending to know what you just said.)