Executing functions parallelly in PHP

Posted by binaryLV on Stack Overflow See other posts from Stack Overflow or by binaryLV
Published on 2010-06-08T14:37:25Z Indexed on 2010/06/08 14:42 UTC
Read the original article Hit count: 204

Filed under:
|

Hi!

Can PHP call a function and don't wait for it to return? So something like this:

function callback($pause, $arg) {
    sleep($pause);
    echo $arg, "\n";
}

header('Content-Type: text/plain');
fast_call_user_func_array('callback', array(3, 'three'));
fast_call_user_func_array('callback', array(2, 'two'));
fast_call_user_func_array('callback', array(1, 'one'));

would output

one (after 1 second)
two (after 2 seconds)
three (after 3 seconds)

rather than

three (after 3 seconds)
two (after 3 + 2 = 5 seconds)
one (after 3 + 2 + 1 = 6 seconds)

Main script is intended to be run as a permanent process (TCP server). callback() function would receive data from client, execute external PHP script and then do something based on other arguments that are passed to callback(). The problem is that main script must not wait for external PHP script to finish. Result of external script is important, so exec('php -f file.php &') is not an option.

© Stack Overflow or respective owner

Related posts about php

Related posts about parallel-processing