Skip to content

Instantly share code, notes, and snippets.

@eddy8
Created March 19, 2021 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddy8/9c4296293510abf134a25391bda3f34b to your computer and use it in GitHub Desktop.
Save eddy8/9c4296293510abf134a25391bda3f34b to your computer and use it in GitHub Desktop.
swoole multi process
<?php
use Swoole\Process;
$filePaths = file_get_contents('./data/origin_url.txt');
$pathArr = explode("\n", $filePaths);
$pathArrSplit = array_chunk($pathArr, 5000);
$processNum = count($pathArrSplit);
foreach ($pathArrSplit as $data) {
$process = new Process(function () use ($data, $resultFile) {
echo 'Child #' . getmypid() . " start" . PHP_EOL;
$i = 0;
foreach ($data as $path) {
// here is your business logic
$i++;
if ($i % 1000 === 0) {
echo 'Child #' . getmypid() . ': ' . $i . PHP_EOL;
}
}
echo 'Child #' . getmypid() . ' exit' . PHP_EOL;
});
$process->start();
}
for ($n = $processNum; $n--;) {
$status = Process::wait(true);
echo "Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}" . PHP_EOL;
}
echo 'over';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment