mrden / forker
Process forker
Requires
- php: ^7.3 || ^8.0
- ext-intl: *
- ext-json: *
- ext-mbstring: *
- ext-pcntl: *
- ext-posix: *
- ramsey/uuid: 3.*
Requires (Dev)
- friendsofphp/php-cs-fixer: 3.4.0
- phpunit/phpunit: ^9
- vimeo/psalm: ^4.30
README
Install
composer require mrden/forker
Fork single callable process
$context = 'any parent code context data'; $callable = new \Mrden\Forker\Process\CallableProcess(static function () use ($context) { echo 'context from parent process ' . $context; }); $forker = new \Mrden\Forker\Forker($callable); $forker->run(); // any code in this parent process
Fork single process of your implementation in 3 clones
namespace Any; class SingleProcess extends \Mrden\Forker\Contracts\Process { use \Mrden\Forker\Traits\ProcessFileStorageTrait; public function execute(): void { echo 'context from parent process ' . ($this->params['context'] ?? ''); } protected function prepare(): void { } protected function checkParams(): void { } }
Start in code
$context = 'any parent code context data'; $singleProcess = new \Any\SingleProcess([ 'context' => $context ]); $forker = new \Mrden\Forker\Forker($singleProcess); $forker->run(3); // any code in this parent process
Start via bin/forker
php bin/forker --process="\Any\SingleProcess" --count=3 --process-context="any context data"
Stop via bin/forker
php bin/forker --process="\Any\SingleProcess" --stop=1 --process-context="any context data"
Stop only 2 clones via bin/forker
php bin/forker --process="\Any\SingleProcess" --stop=1 --count=2 --process-context="any context data"
Stop only 2-nd clone via bin/forker
php bin/forker --process="\Any\SingleProcess" --stop=1 --clone_number=2 --process-context="any context data"
Restart all clones via bin/forker
php bin/forker --process="\Any\SingleProcess" --restart=1 --process-context="any context data"
Restart only 2 clones via bin/forker
php bin/forker --process="\Any\SingleProcess" --restart=1 --count=2 --process-context="any context data"
Restart only 2-nd clone via bin/forker
php bin/forker --process="\Any\SingleProcess" --restart=1 --clone_number=2 --process-context="any context data"