norberttech / symfony-process-executor
强化版的Symfony进程组件,支持异步/同步执行链。
2.1.0
2022-01-01 20:40 UTC
Requires
- php: ^7.4.2 || ^8.0 || ^8.1
- aeon-php/sleep: >=0.6.0
- symfony/process: ^3.4|^4.4|^5.0
This package is auto-updated.
Last update: 2024-09-20 01:09:10 UTC
README
这是一个小型库,简化了并行(或非并行)启动多个进程的过程。
安装
composer require norberttech/symfony-process-executor
示例
<?php use NorbertTech\SymfonyProcessExecutor\AsynchronousExecutor; use NorbertTech\SymfonyProcessExecutor\ProcessPool; use NorbertTech\SymfonyProcessExecutor\ProcessWrapper; use Symfony\Component\Process\Process; $processes = new ProcessPool( Process::fromShellCommandline('sleep 1 && echo 1'), Process::fromShellCommandline('sleep 2 && echo 2'), Process::fromShellCommandline('sleep 3 && echo 3'), Process::fromShellCommandline('sleep 4 && echo 4'), Process::fromShellCommandline('sleep 5 && echo 5') ); $executor = new AsynchronousExecutor($processes); $executor->execute(); $executor->waitForAllToFinish(); $executor->pool()->each(function (ProcessWrapper $processWrapper) { var_dump($processWrapper->exitCode()); var_dump(\trim($processWrapper->output())); var_dump($processWrapper->executionTime()->inSeconds()); var_dump($processWrapper->executionTime()->inMilliseconds()); var_dump($processWrapper->executionTime()->microsecond()); echo "----\n"; }); echo \sprintf("Successfully finished child processes: %d\n", $executor->pool()->withSuccessExitCode()); echo \sprintf("Failure finished child processes: %d\n", $executor->pool()->withFailureExitCode()); echo \sprintf("Total execution time [s]: %d\n", $executor->executionTime()->inSecondsPreciseString());
输出
php examples/async_multiple_success_processes.php int(0) string(1) "1" int(1) int(1033) int(1033295) ---- int(0) string(1) "2" int(2) int(2064) int(2064680) ---- int(0) string(1) "3" int(3) int(3092) int(3092137) ---- int(0) string(1) "4" int(4) int(4026) int(4026060) ---- int(0) string(1) "5" int(5) int(5052) int(5052531) ---- Successfully finished child processes: 5 Failure finished child processes: 0 Total execution time [s]: 5
测试
此库的所有测试均以phpt文件编写,您可以在以下链接中了解更多信息: https://qa.php.net/phpt_details.php。
要运行完整的测试套件,请使用composer。
composer tests