1. Go to this page and download the library: Download mensbeam/fork library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mensbeam / fork example snippets
use MensBeam\Fork;
function gen(): \Generator {
foreach (range(1, 5) as $n) {
yield function () use ($n) {
$delay = rand(1, 5);
sleep($delay);
return [ $n, $delay ];
};
}
}
(new Fork())->after(function(array $output) {
echo "{$output['data'][0]}: {$output['data'][1]}\n";
})->run(gen());
use MensBeam\Fork;
(new Fork())->concurrent(2)->run([
fn() => sleep(1),
fn() => sleep(1),
fn() => sleep(1)
]);
use MensBeam\Fork;
(new Fork())->timeout(5)->run([
fn() => sleep(10), // This will timeout
fn() => sleep(2)
]);
use MensBeam\Fork;
$f = new Fork();
$f->after(function(array $output) use ($f) {
if ($output['data'] === 'stop') {
$f->stop();
}
})->run([
fn() => 'continue',
fn() => 'stop',
fn() => 'never runs'
]);
use MensBeam\Fork;
(new Fork())->after(function(array $output) {
if ($output['data'] instanceof MensBeam\Fork\ThrowableContext) {
echo "Child failed with: " . $output['data']->getMessage() . "\n";
} else {
echo "Child succeeded with: " . $output['data'] . "\n";
}
})->run([
fn() => throw new \RuntimeException("Something went wrong!"),
fn() => "All good"
]);
use MensBeam\Fork;
Fork::$tracesInThrowableContexts = true;
use MensBeam\Fork;
Fork::$throwInFork = true;
(new Fork())->after(function(array $output) {
echo "Ook!\n";
})->run([
fn() => throw new \RuntimeException('Eek!'))
]);
PHP Fatal error: Uncaught RuntimeException: Eek! in /path/to/test.php:42
Ook!
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.