PHP code example of kriss / multi-process
1. Go to this page and download the library: Download kriss/multi-process library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kriss / multi-process example snippets
use Kriss\MultiProcess\MultiProcess;
use Kriss\MultiProcess\PendingProcess;
use Symfony\Component\Process\Process;
// 调用系统命令
$results = MultiProcess::create()
->add('hostname')
->add(new Process(['ls']))
->add(Process::fromShellCommandline('echo 123'))
->add(
PendingProcess::fromShellCommandline('pwd')
->setStartCallback(function ($type, $buffer) {
echo $buffer;
})
);
var_dump($results->getOutputs());
// 调用 PHP 代码
$value = 123;
$results = MultiProcess::create()
// callback
->add(function () use ($value) {
// 支持 use 和 $this 使用
return $value;
})
// 调用 SomeClass::method
//->add([SomeClass::class, 'method'])
->wait();
var_dump($results->getOutputs());
use Kriss\MultiProcess\MultiProcess;
$filename = 'https://www.example.com/';
$startTime = microtime(true);
for ($i = 0; $i < 3; $i++) {
file_get_contents($filename);
}
echo ('use time: ' . round(microtime(true) - $startTime, 6)) . PHP_EOL; // 3秒以上
for ($i = 0; $i < 3; $i++) {
$processes[] = PendingProcess::fromShellCommandline("php -r \"echo file_get_contents('$filename');\"");
}
$startTime = microtime(true);
MultiProcess::create()
->addMulti($processes)
->wait();
echo ('use time: ' . round(microtime(true) - $startTime, 6)) . PHP_EOL; // 1秒多