Sorry guys – an actual tech post so those of you who don’t want to can read the previous post and dig through Daisy Owl’s archives until I’m done. For the rest of you? Read on…
I’ve been doing a load of work in PHP lately which errs on the advanced side of the language in certain areas. Specifically I’ve been working with the PHP Command Line Interpreter (CLI) to build a backend application on a par with a traditional Linux Daemon. Of course PHP presents a number of barriers to Daemon development and not least amongst them is a lack of support for threading. Certainly for highly blocking, spool intensive apps (like the one I’m trying to write) the facility to spawn multiple threads would be useful.
Yes I could use an alternative platform. Python, Perl, Java all have excellent threading support but my level of familiarity with PHP makes it the easiest choice even when I have to come up with some pretty round the houses solutions to issues.
For the most part I do not feel that threading is quite the panacea it once was. Traditionally, threading has been used to allow multiple threads of processing to run simultaneously on the same processor and to share data / communicate with each other. Forking on the other hand actually creates a new and independent process and has, historically, been considered too computationally expensive to use for intensive applications. With all of that said and understood I would reply that on modern systems, with massively powerful, multi core processors, RAM to spare and interpreted languages coming to the fore for enterprise level applications, forking (which is supported by PHP) becomes a very usable alternative to the preferred threading model.
The rest of the post references other site’s which provide information on Faux Threading techniques and how they can be used under PHP for asynchronous, multi path applications. Please read through and I hope you find them helpful.
Oh and it goes without saying that a large portion of these techniques are only supported on Linux stacks … not that I can think of a reason to run anything else
This initial example provides a good grounding in PHP forking along with introducing the functions used to do so. In this example, Mike uses Forking to parallelise a YouTube downloading script: Basic Forking Example
Of course in the LAMP development world a quick and dirty hack is often as good as a well rounded programmewd solution. Joseph Scott shows you how to use a Linux execution feature (namely forking a command into the background using and ampersand) to parallelise applications that don’t need to return anything. I’ve actually used this technique to build a one hit multiplexor for various applications: Quick and dirty fake fork for non returning scripts
A more polished method actually uses the non blocking stream parameter to permit multi threaded netowrk IO from a PHP script. This is another quick and dirty alternative to full threading: Part 1 and Part 2
In an example that is closer to true threading Brian W Bosh uses STD IO to facilitate basic communication between processes without all of that nasty mucking about in Shared Memory :Faux Threading
In the next post on this topic I’ll be looking at more detailed tutorials for using Forking in anger complete with advanced interprocess communication via Shared Memory and Semaphores and tying it all together with a multiplexor to over see a pool of processes.






No Comments on "Parallel Processing in PHP"