问题:我需要克隆/下载几个 git 存储库,不幸的是按顺序执行需要很长时间。我想到了使用 ReactPhp 事件循环并并行执行。
尽管进行了多次尝试,但我无法让它并行运行。也许我误解了这个概念,但我希望 ReactPhp 以某种方式分叉执行我的代码。
你能看看我的代码并分享一些如何让它工作的指南吗?
use Symfony\Component\Stopwatch\Stopwatch;
include 'vendor/autoload.php';
$toClone = [
['url' => 'http://github.com/symfony/symfony.git', 'dest' => 'C:\tmp\cloneR1'],
['url' => 'http://github.com/laravel/laravel.git', 'dest' => 'C:\tmp\cloneR2'],
['url' => 'http://github.com/rails/rails.git', 'dest' => 'C:\tmp\cloneR3'],
];
$end = count($toClone);
$i = 0;
$deferred = new React\Promise\Deferred();
$fClone = function (\React\EventLoop\Timer\Timer $timer) use (&$i, $deferred, &$toClone, $end) {
$project = array_pop($toClone);
$git = new \GitWrapper\GitWrapper();
$git->setTimeout(3600);
$git->cloneRepository($project['url'], $project['dest']);
$deferred->notify([$i++, $project['url']]);
if ($end <= $i) {
$timer->cancel();
$deferred->resolve();
}
};
$stopwatch = new Stopwatch();
$stopwatch->start('run');
$loop = React\EventLoop\Factory::create();
$loop->addPeriodicTimer(1, $fClone);
$deferred->promise()->then(function () use ($stopwatch) {
echo 'DONE' . PHP_EOL;
$event = $stopwatch->stop('run');
echo 'Run took ' . $event->getDuration() / 1000 . 'sec and ' . $event->getMemory() . ' bytes of memory';
}, null, function ($data) {
echo 'RUN ' . $data[0] . ' - ' . $data[1] . PHP_EOL;
});
$loop->run();
我的 composer.json
{
"require": {
"react/promise": "2.2.0",
"react/event-loop": "0.4.1",
"cpliakas/git-wrapper": "1.4.1",
"symfony/stopwatch": "2.7.0"
}
}
操作系统:Windows7 PHP:5.4.8 和 5.5.20
没有安装这些扩展
"suggest": {
"ext-libevent": ">=0.1.0",
"ext-event": "~1.0",
"ext-libev": "*"
},
所以使用了 StreamSelectLoop