1. Go to this page and download the library: Download fiasco/async 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/ */
fiasco / async example snippets
use Async\ForkManager;
use Async\ForkInterface;
use Async\Exception\ChildExceptionDetected;
$forkManager = new ForkManager();
// Return an instance of ForkInterface.
// Will be either a SynchronousFork or an AsynchronousFork if PCNTL extension
// is enabled.
$fork = $forkManager->create();
// This anonymous function will run inside a child fork.
$fork->run(function (ForkInterface $fork) {
return file_get_contents('https://google.com/');
});
// Return contents from run function will be provided as the input argument
// to the onSuccess callback. This anonymous function is run in the parent thread.
$fork->onSuccess(function ($html) {
echo "Got website with a length of ".strlen($html)." bytes\n";
});
// On error is called when the child process encounters an uncaught throwable.
$fork->onError(function (ChildExceptionDetected $e) {
echo "ERROR: ".$e->getMessage().PHP_EOL;
});
// Block on main thread until all forks have completed.
$forkManager->awaitForks();
use Async\ForkManager;
use Async\ForkInterface;
use Async\Exception\ChildExceptionDetected;
$forkManager = new ForkManager();
$forkManager->create()
->run(function (ForkInterface $fork) {
return file_get_contents('https://google.com/');
})
->onSuccess(function ($html) {
echo "Got website with a length of ".strlen($html)." bytes\n";
})
->onError(function (ChildExceptionDetected $e) {
echo "ERROR: ".$e->getMessage().PHP_EOL;
});
// Block on main thread until all forks have completed.
$forkManager->awaitForks();
use Async\ForkManager;
$forkManager = new ForkManager();
// Wait up to 3 seconds for fork response before giving up.
$forkManager->setWaitTimeout(3);
// This will result in a caught fatal error from the child.
$forkManager->create()->run(fn() => @$foo->doNothing());
// This fork will catch a thrown exception and trigger the onError callback
// in the parent thread.
$forkManager->create()->run(function (ForkInterface $fork) {
// You can set labels inside the fork to give better information about
// what the specific fork is doing.
$fork->setLabel("Fork that throws an error.");
throw new \Exception('bar');
});
// Waiting for the fork in parent will timeout and force the fork
// to error out. The Fork child will still attempt to return the
// result but it will fail.
$forkManager->create()->run(function (ForkInterface $fork) {
$fork->setLabel("Fork that will timeout.");
sleep(4);
return 'fuz';
});
$forkManager->create()->run(fn() => 'baz');
$forkManager->awaitForks();
// Loop over errored forks.
foreach ($forkManager->getForks(ForkInterface::STATUS_ERROR) as $fork) {
// Result will be instance Async\Exception\ChildExceptionDetected.
echo sprintf("Fork '%s' encountered an error:\n", $fork->getLabel());
// echo $fork->getResult()->getMessage() . PHP_EOL;
}
foreach ($forkManager->getForks(ForkInterface::STATUS_COMPLETE) as $fork) {
// Result will be instance Async\Exception\ChildExceptionDetected.
echo sprintf("Fork '%s' completed.\n", $fork->getLabel());
// echo $fork->getResult()->getMessage() . PHP_EOL;
}
use Async\ForkInterface;
// .. create forks.
// Wait for all forks to complete.
$forkManager->awaitForks();
// Loop over errored forks.
foreach ($forkManager->getForks(ForkInterface::STATUS_ERROR) as $fork) {
// Result will be instance Async\Exception\ChildExceptionDetected.
echo sprintf("Fork '%s' encountered an error:\n", $fork->getLabel());
echo $fork->getResult()->getMessage() . PHP_EOL;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.