PHP Multithreading – Faking It

PHP doesn’t really support multi-threading per se but there are ways to do “fake” multithreading. Here’s one I saw in the PHPClasses.org newsletter – Multi-thread Simulation.

Note that this class is intedend for use on a webserver, as opposed to running PHP scripts from a command line (or similar). Check the end of this post for some alternatives you can try if you’re using PHP as a stand-alone scripting language.

Now, I’m going to be lazy and just copy the description of the class from the project page :)

This class can emulate the execution of program threads using separate HTTP requests to the same script.

It establishes an HTTP connection to the same Web server to execute the same PHP script. It sends a request passing the name a function to execute and an argument to be passed to that function.

The requested script executes some code that detects the thread execution request and calls the specified function.

When the thread request script ends, the return values of the called function is returned as a serialized string.

The calling script can execute other tasks while the thread script runs. The results may be collected later when the thread script ends.

Alternatives

PHP Process Control Functions (via pcntl_fork())
PCNTL lets you do simple multithreading “*nix-style” – you can fork the current process and work from there. However, this extension is not enabled by default and it most likely wouldn’t be enabled on a webserver, too.

Multi-threading strategies in PHP
Edit: The linked site appears to be down.

So there you have it. Personally I don’t need multi-threading in any of my current PHP projects, but it seems like a useful thing to note for future reference.

Related posts :

15 Responses to “PHP Multithreading – Faking It”

  1. White Shadow says:

    No, but I’ll take a look at it today.

  2. White Shadow says:

    You got lucky there, my gut reaction to a comment containing nothing but link(s) is to mark it as spam ;)

    The link is pertinent though. Thanks for the info.

  3. Free Games says:

    Sonic looks like it has potential but I’m not entirely sure how it works. Are there any howto guides? I’m looking for a way to have multiple PHP instances on a single webserver.

  4. White Shadow says:

    I’ve got nothing.

  5. ;) says:

    please provide me with an example

  6. White Shadow says:

    I’m afraid in this case you’ll have to figure it out yourself :P

  7. swan says:

    write the author? sure he/she might help you if the you barter to write up some docs for others (eg. specific uses of etc)

  8. anon says:

    usually youre forced to write thread management when you cant fork. fork threads internally on the OS level as it is a whole new process. unless you want to thread a count or code in _the_middle_ with PHP you are only adding more overhead. you can still clean up your children with forks. forking has abstractedly removed thread management.

  9. العاب says:

    I’m going to have multi-threading in a project that I’m working on now, i thought it’s impossible to do this thing before, but by searching the internet i found many help including this post. I think i could start working on this project now.

  10. [...] Threading support in PHP is so-so, and the pcntl extension doesn’t work on Windows. Since I’m too lazy to boot up Linux just for the sake of PHP development, I once again went looking for ways to simulate threads (I’ve written about this before). [...]

  11. anon says:

    pcntl does work on windows, with cygwin

Leave a Reply