swoole 多进程案例

执行一段代码 :

模拟执行(curlData)这段代码耗时1秒

如果执行3次 单进程需要3秒 现在让一秒执行完

echo "start:".date('Y-m-d H:i:s').PHP_EOL;
$workers = [];
$urls = [
    'https://baidu.com',
    'https://qq.com',
    'https://sina.com.cn',
];
for($i=0;$i<3;$i++){
    $process = new swoole_process(function($process)use($i,$urls){
        $content = curlData($urls[$i]);
        echo $content.PHP_EOL;
        //$process->exec("/usr/local/php/bin/php");
    }, true);
    $pid = $process->start();
    $workers[$pid] = $process;
}
foreach ($workers as $process){
    echo $process->read();
}
function curlData($url){
    sleep(1);
    return $url."success".PHP_EOL;
}
echo "end:".date('Y-m-d H:i:s').PHP_EOL;

 

你可能感兴趣的:(Swoole)