Main Content

Parallel processing in PHP

Archive - Originally posted on "The Horse's Mouth" - 2007-01-03 17:40:03 - Graham Ellis

PHP is designed - where necessary to the exclusion of other uses - as a web page tool. In other words, where a language design decision needs to made between something that is good for the web use, but bad for more general use, the decision will always be in favour of the web. "I don't care how it effects the other users" said Rasmus Lerdorf when I attended a lecture of his a year or two back - but what Rasmus (the PHP originator and mentor) means is that he really does care that it should be great for web users.

So ... most of the other language I teach had a "fork" function or equivalent from the early days which lets you split the process in two, with each half performing a different task. In PHP, it's there too - as from 4.1 - under the name pcntl_fork. But forking a complete process can be resource-heavy and tricky to code and there are other options too - such as setting off parallel actions on pipes.

Here's some sample code links.

Pinging multiple hosts in parallel
Grabbing titles from multiple pages in parallel
You can try the second example here if you wish.

You'll notice that I've used the full path to PHP and my test script in the title grabber. That's because the PHP script will be called up with goodness only knows what path and current directory, and it's sometimes needed to get the thing to work. You may also find that some of the php.ini settings hamper you.

Finally, a note of caution. If you write a script that goes out and grabs lots of information from other servers, you're writing an automata or robotic process and you should check that this is acceptable to your target site - their robots.txt file.