PHP Mutilprocess
This is high performance PHP Mutilprocessing Task Manager written in PHP, compatible with PHP-FPM and CLI.
这是一个用PHP编写的高性能PHP多进程异步任务管理器,与PHP FPM和CLI兼容。
Contents
Feature
-
By using this tool, PHP scripts can be invoked asynchronously based on multi processes, and finally wait for each process to return results, saving lots of time., (*1)
-
graceful and efficient, (*2)
-
can get callable function return value, (*3)
-
You can specify the PHP path for asynchronous execution scripts by specifying the phpBin parameter., (*4)
-
Exception Handling, (*5)
Installation
You can use composer to install this library from the command line., (*6)
composer require sinacms/multiprocess
Usage
distribute tasks and async execute
<?php
use Mutilprocessing\Async;
Async::create()->start('task.php', ['runTest'.$i]);
distribute tasks by a simple function and async execute
!!! WARN:please don't insert a '&&&' string in echo and return for some reason it will break down the program run, (*7)
<?php
use Mutilprocessing\Async;
Async::create()->startFunc(function($param1, $param2) {
echo $param1.$param2.PHP_EOL;
}, ['param1' => 'hello', 'param2' => ' world']);
$func = function ($param1, $param2) {
echo "this is an another func";
};
Async::create()->startFunc($func, ['param1' => 'hello', 'param2' => ' PHP']);
sync wait for all process end
<?php
use Mutilprocessing\Async;
$outData = [];
Async::wait(function($code, $out, $err) use(&$outData) {
// var_dump($code, $out, $err);
// you can handle code runtime exception like this
if (strlen($err) != 0) {
// do sth.
}
// and you can get return value like this
// more function detail see examples :)
array_push($outData, \Mutilprocessing\Async::getReturn($out));
});
outData structure:, (*8)
echos represent echos in execute, (*9)
returns represent return in execute, (*10)
array(4) {
[0] =>
array(2) {
'echos' =>
string(5) "hello"
'returns' =>
string(0) ""
}
[1] =>
array(2) {
'echos' =>
string(6) "KZ RNG"
'returns' =>
string(10) "return 777"
}
[2] =>
array(2) {
'echos' =>
string(17) "EDG AFSreturn 888"
'returns' =>
string(0) ""
}
[3] =>
array(2) {
'echos' =>
string(6) "SKT RW"
'returns' =>
string(10) "return 666"
}
}
getArgs passed from Async::start
// please pass $argv[1] to get args
<?php
use Mutilprocessing\Async;
Async::getArgs($argv[1], 'key');
clean all task
<?php
use Mutilprocessing\Async;
Async::discard();
Documentation
- ### Async
- public static function create()
- public static function start($scriptname, $args, $phpBin="", $envs = [])
- public function startFunc(callable $function, $args = [], $phpBin="")
- public static function discard()
- public static function wait(callable $logHandler = null)
- public static function getArgs($argv = null)
- public static function getReturn($out)
- ### FunctionParser
- public static function genTmp(callable $function)
Todo
- regCallback for each execution (on process)
- add multi execution unit and start once