Created
March 19, 2021 05:53
-
-
Save eddy8/9c4296293510abf134a25391bda3f34b to your computer and use it in GitHub Desktop.
swoole multi process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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