uppes / processor
1.4.3
2020-03-03 01:21 UTC
Requires
- php: >7.2
- opis/closure: ^3.5.1
- symfony/process: ^5.0.4
Requires (Dev)
- phpunit/phpunit: ^6 | ^7 | ^8
README
一个简单的symfony/process进程管理器包装API,用于执行和管理子进程。
当未安装pcntl扩展时,它是我们的symplely/coroutine包的一部分,用于处理不被原生Coroutine处理的任何阻塞I/O进程。
该库旨在为Windows OS和其他系统提供易于使用的API,以控制/管理子进程,而无需安装任何额外的软件扩展。
安装
composer require symplely/processor
用法
include 'vendor/autoload.php'; use Async\Processor\Processor; // To set the path to PHP executable for child process Processor::phpPath('/some/path/version-7.3/bin/php'); $process = \spawn($function, $timeout, $channel) // Or $process = Processor::create(function () use ($thing) { // Do a thing }, $timeout, $channel) ->then(function ($output) { // Handle success })->catch(function (\Throwable $exception) { // Handle exception }); \spawn_run($process); // Or $process->run(); // Second option can be used to set to display child output, default is false \spawn_run($process, true); // Or $process->displayOn()->run();
通道 - 在进程之间传输消息
include 'vendor/autoload.php'; use Async\Processor\Channel; use Async\Processor\ChannelInterface; $ipc = new Channel(); $process = spawn(function (ChannelInterface $channel) { $channel->write('ping'); // same as echo 'ping' or echo fwrite(STDOUT, 'ping') usleep(1000); echo $channel->read(); // same as echo fgets(STDIN); echo $channel->read(); usleep(1000); return 'return whatever'; }, 300, $ipc) ->progress(function ($type, $data) use ($ipc) { if ('ping' === $data) { $ipc->send('pang' . \PHP_EOL); } elseif (!$ipc->isClosed()) { $ipc->send('pong' . \PHP_EOL); ->close(); } }); $ipc->setup($process) \spawn_run($process); echo \spawn_output($process); // pingpangpongreturn whatever // Or echo $ipc->receive(); // return whatever
事件钩子
在创建异步进程时,将返回一个LauncherInterface
实例。您可以在进程中添加以下事件钩子。
$process = spawn($function, $timeout, $channel) // Or $process = Processor::create(function () { // The second argument is optional, Defaults 300. // it sets The maximum amount of time a process may take to finish in seconds // The third is optional input pipe to pass to subprocess }, int $timeout = 300 , $input = null) ->then(function ($output) { // On success, `$output` is returned by the process. }) ->catch(function ($exception) { // When an exception is thrown from within a process, it's caught and passed here. }) ->timeout(function () { // When an time is reached, it's caught and passed here. }) ->progress(function ($type, $data) { // A IPC like gateway: `$type, $data` is returned by the process progressing, it's producing output. // This can be use as a IPC handler for real time interaction. });
还包括->done
,它是->then()
扩展回调方法的一部分。
->done(function ($result) { // On success, `$result` is returned by the process or callable you passed to the queue. }); ->then(function ($resultOutput) { // }, function ($catchException) { // }, function ($progressOutput) { // } ); // To turn on to display child output. ->displayOn(); // Stop displaying child output. ->displayOff(); // To display child output, only by third party means once turned on. ->display(); // Processes can be retried. ->restart(); ->run();
错误处理
如果子进程中抛出了Exception
或Error
,可以在->catch()
方法中指定回调来按进程捕获。
如果没有添加错误处理程序,则在调用spawn_run()
或$process->run()
时,错误将在父进程中抛出。
如果子进程在没有抛出可抛出异常的情况下意外停止,则写入stderr
的输出将被包装,并在父进程中以Async\Processor\ProcessorError
的形式抛出。
贡献
鼓励并欢迎贡献;我总是很高兴在GitHub上得到反馈或pull请求 :) 为错误和新特性创建GitHub Issues并评论您感兴趣的。
许可
MIT许可(MIT)。请参阅许可文件以获取更多信息。