symfony-bundles / fork
SymfonyBundles Fork 库
v3.5.0
2018-02-26 20:27 UTC
Requires
- php: ^7.1.3
Requires (Dev)
- phpunit/php-code-coverage: ^5.0
- phpunit/phpunit: ^6.4
README
安装
使用composer安装库
composer require symfony-bundles/fork
如何使用(仅限cli模式)
创建fork服务
use SymfonyBundles\Fork; $fork = new Fork\Fork();
创建一个实现接口 SymfonyBundles\Fork\TaskInterface
的任务。例如
namespace AppBundle\Task; use SymfonyBundles\Fork\TaskInterface; class DemoTask implements TaskInterface { public function execute() { echo "Hello World!\n"; } }
现在任务已创建,您可以在多个进程中执行它
use AppBundle\Task\DemoTask; $task = new DemoTask(); $fork->attach($task)->run(4)->wait(); // 4 - this is number of subprocesses
另一个示例
$task1 = new DemoTask(); $task2 = new DemoTask(); $task3 = new DemoTask(); $fork->attach($task1)->attach($task2)->attach($task3); $fork->run(); // by default, the optimal number of subprocesses will be determined $fork->wait();
如果您调用方法 wait
,当前进程(主进程)将等待,直到所有子进程完成。