Getting Started
To run some code in a thread just pass a callable to the call()
method:
$fork->call("phpinfo");
The code should start executing right away, and pass control back to your script. Here’s a simple example demonstrating how the code runs side by side:
$fork->call(function () {
for ($i = 1; $i <= 3; $i++) {
echo "Process A - " . $i . "\n";
sleep(1);
}
});
$fork->call(function () {
for ($i = 1; $i < 3; $i++) {
echo "Process B - " . $i . "\n";
sleep(1);
}
});
sleep(1);
echo "Waiting for the threads to finish...\n";
$fork->wait();
echo "End\n";
This should output something similar to the below:
Process A - 1
Process B - 1
Waiting for the threads to finish...
Process A - 2
Process B - 2
Process A - 3
End
The call()
method supports variadic arguments:
$fork->call("doStuffThatTakesALongTime", "5897", time());
# Is the same as:
doStuffThatTakesALongTime("5897", time());