Parallel processes in PHP

Posted February 12th, 2009 by Juozas

When I was coding Ray-Tracer project for my Computer Science studies in university, I ran into using Haskell parallel map function (map calls function for all list elements). Ray-Tracer runs reflections, shadows, ray-casts, etc. detection for every single pixel in scene and since everything is mathematical calculations, it’s paralleling is almost trivial.

Parallel map functions does all work for you – you don’t even need to know when it’s right time to fork another thread. Implementing parallel computation in Haskell was very easy and it almost gave theoretical decrease in processing time by N times (where N is processor cores count). Today I will talk a little bit about possible parallelism with PHP’s internal POSIX functions.

PHP is neither a functional language, nor is made with threading support (correct me if I’m wrong). Still, it’s possible to create parallel processes by using pcntl_* functions family. Some days ago I tried just to get something working, what uses parallel processes and can possible be extended to deal with time-consuming mathematical algorithms.

Download my sample code here (rename extension to php) and run it in terminal (it probably wont run with Apache). This code will try to compute x^3 for 0-1M integers two times, parallel runs both in one call, normal behaviour runs one after another. Output should look similar to this:

juokaz@thinkpad:~/Desktop/php/parallel$ php run.php
We start
I am the child, pid = 0
I am the parent, pid = 2004
yep, finished, I have 2004 ID
yep, finished, I have 0 ID
Ran parallel 5.81009602547 seconds
yep, finished, I have 1 ID
yep, finished, I have 2 ID
Ran normal 9.76671719551 seconds

As you can see, parallel computation on dual core processor ran almost 2x faster. And yes, it does use both cores simultaneously.

If you want to look more deeply, there is great article called Thorough look at PHP’s pcntl_fork(). You will quite quickly find that there are problems when using pcntl_fork, because it basically clones running process and then continues. Everything you define before forking child process will be accessible inside child – it’s not always what you really want.

Parallel processes can be easily simulated by using asynchronous calls (over HTTP, for example) from one script to others, but for something more calculations-based, using pcntl_fork() can be much more practical. But I will probably still chose asyncronous calls over parallelizing because they are much more flexible.

Trackbacks/Pingbacks

  1. 網站製作學習誌 » [Web] 連結分享

Comments (10)

  1. EllisGL

    A better way to emulate threading is use use sockets and the exec command. Your main program would be a server daemon and then it would exec PHP from shell to kick off another process with some CLI arguments. This would tell the “children threads” how to communicate to the “parent”. Also with some tweaking you could do “distributed computing”.

  2. Juozas (author)

    Yes, in PHP this is probably the best way – real parallelism is not very good.

    I have “host php” -> “child phps” approach implemented for CRON tasks. I’m not always have access to cron tasks config, so I use php file, which is invoked every minute by cron. That file simulates cron’s functionality and calls required scripts.

  3. Phill Pafford

    EllisGL Could you give an example or link?

    Looking to find the best possible way for multi threading in PHP.

  4. Juozas (author)

    Hi Phill,

    maybe this package: http://www.phpclasses.org/browse/package/4082.html
    or: http://devzone.zend.com/article/3341-Multithreading-in-PHP-with-CURL ?

  5. EllisGL

    I had found a great example before, but can’t find it..
    Here’s something close:
    http://home.jayhaabee.nl/trac/

  6. EllisGL

    Er – You have to find something talking about threading and IPC with PHP.. That will give you the biggest part of the puzzle.

  7. henk

    On the hand it’s good that someone of the PHP camp at least starts thinking a little about doing stuff in parallel in PHP. On the hand, for all but the most basic use case scenarios PHP still has a long way to go.

    Traditional imperative languages like Java or C# have had parallelism build in right from the start and have been gradually improving on this over the years. A nice presentation about parallel library support for imperative languages is this one:

    http://www.parleys.com/display/PARLEYS/Home#slide=3;title=From%20Concurrent%20to%20Parallel;talk=24576007

    Ok, the language uses there is Java, but the concept in general enough to be interesting to users of other languages. I wonder though if anything like the level of support discussed in that presentation is on the radar of the PHP development team. Is the PHP target audience even interested is such far reaching concurrency utils/tools/frameworks?

  8. Juozas (author)

    Is the PHP target audience even interested is such far reaching concurrency utils/tools/frameworks?

    Probably not :) But since I’m not doing degree in CS now, I like to experiment with things.

  9. EllisGL

    @Henk: PHP was definitely not designed for task of such a nature. But we have come up with creative ways to achieve what was thought to be impossible for PHP to do. Yes I would like to PHP like programing language branch off that is more like C / C++ / Java in respects of these lower level accesses.

  10. Scott

    @Henk: We are an application service provider and our codebase has hundreds of thousands of lines of PHP code in it. When we need to write supporting utilities or automated behind-the-scenes tasks, we develop them in PHP because, well, why reinvent the wheel? All of our database models, library functions, classes, etc all exist in PHP. From this perspective, you can see why having these types of tools/features is very helpful and even necessary.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">