1. Go to this page and download the library: Download thelevti/phpfork 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/ */
thelevti / phpfork example snippets
use TheLevti\phpfork\Fork;
use TheLevti\phpfork\ProcessManager;
use TheLevti\phpfork\SharedMemory;
$manager = new ProcessManager();
$fork = $manager->fork(function (SharedMemory $shm) {
// Do something in a forked process!
return 'Hello from ' . posix_getpid();
})->then(function (Fork $fork) {
// Do something in the parent process when the fork is done!
echo "{$fork->getPid()} says '{$fork->getResult()}'\n";
});
$manager->wait();
use TheLevti\phpfork\ProcessManager;
use SplFileInfo;
$files = new RecursiveDirectoryIterator('/path/to/images');
$files = new RecursiveIteratorIterator($files);
$manager = new ProcessManager();
$batchJob = $manager->process($files, function(SplFileInfo $file) {
// upload this file
});
$manager->wait();
use Doctrine\DBAL\DriverManager;
use TheLevti\phpfork\Batch\Strategy\ChunkStrategy;
use TheLevti\phpfork\EventDispatcher\Events;
use TheLevti\phpfork\EventDispatcher\SignalEventDispatcher;
use TheLevti\phpfork\ProcessManager;
$params = array(
'dbname' => '...',
'user' => '...',
'password' => '...',
'host' => '...',
'driver' => 'pdo_mysql',
);
$forks = 4;
$dataArray = range(0, 15);
$callback = function ($value) use ($params) {
// Child process acquires its own DB connection
$conn = DriverManager::getConnection($params);
$conn->connect();
$sql = 'SELECT NOW() AS now';
$stmt = $conn->prepare($sql);
$stmt->execute();
$dbResult = $stmt->fetch();
$conn->close();
return ['pid' => getmypid(), 'value' => $value, 'result' => $dbResult];
};
// Get DB connection in parent
$parentConnection = DriverManager::getConnection($params);
$parentConnection->connect();
$dispatcher = new SignalEventDispatcher();
$dispatcher->addListener(Events::PRE_FORK, function () use ($parentConnection) {
$parentConnection->close();
});
$manager = new ProcessManager($dispatcher, null, true);
/** @var TheLevti\phpfork\Fork $fork */
$fork = $manager->process($dataArray, $callback, new ChunkStrategy($forks));
$manager->wait();
$result = $fork->getResult();
// Safe to use now
$sql = 'SELECT NOW() AS now_parent';
$stmt = $parentConnection->prepare($sql);
$stmt->execute();
$dbResult = $stmt->fetch();
$parentConnection->close();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.