Application 1: Request interface, reduce time

If we run CURL synchronously, it will take at least 3 seconds to complete, which is very bad for the user experience. We can introduce swoole’s process child to start three processes at the same time with CURL, which takes 1 second to complete.

Code implementation:

<? phpecho 'start:' . date("Ymd H:i:s");
$urls = [
    'http://www.baidu.com'.'http://www.sina.com.cn'.'http://www.qq.com',]; /*foreach ()$urls as $url) {
    $content = [] = file_get_contents($url); }*/ // use swoole's process to start multiple subprocessesfor ($i = 0; $i < 3; $i++) {// Use is used to pass values$process = new swoole_process(function (swoole_process $worker) use ($i.$urls) {//curl curl$content = curlData($urls[$i]); // Because the following arguments aretrueSo it goes into the pipeecho $content . PHP_EOL;
    }, true);
    $pid = $process->start();
    $wokers[$pid] = $process;

}

foreach ($wokers as $process) {
    echo $process->read(a); }function curlData($url) {// Simulation consumes 1 second sleep(1);return $url . "success" . PHP_EOL;
}

echo 'end:' . date("Ymd H:i:s");
Copy the code

The result of the run is as follows, and indeed the run is shortened to 1 second


Application 2: Send emails and text messages

For example, we need to determine whether we need to send emails and SMS messages to known user data, and if we need to send them.

When multiple processes are not used, we first decide whether to send the mail or not, and then send it if necessary. Then determine whether to send a text message, and if so. If it takes 2 seconds to send an email and 2 seconds to send a text, then it takes us about 4 seconds to complete the task.

If we use multiple threads, we can open two threads, one for email and one for SMS, and the total processing time is about 2s, which cuts the processing time in half.

Let’s look at the detailed code

<? php$info = array(
    "sendmail"= > 1,"mailto"= >"[email protected]"."sendsms"= > 1,"smsto"= >"123456"
);
echo "start:".date("Y-m-d H:i:s").PHP_EOL;
$mail_process = new swoole_process('sendMail'.true);
$mail_process->start();
$sms_process = new swoole_process('sendSMS'.true);
$sms_process->start(); // The main process outputs the subprocess scopeecho $mail_process->read(a);echo PHP_EOL;
echo $sms_process->read(a);echo PHP_EOL;
echo "end:".date("Y-m-d H:i:s").PHP_EOL; // Parallel functionfunction sendMail(swoole_process $worker){
    global $info;
    if($info['sendmail']==1){
        sleep(2);
        $worker->write("send mail to ".$info['mailto']); }}function sendSMS(swoole_process $worker){
    global $info;
    if($info['sendmail']==1){
        sleep(2);
        $worker->write("send sms to ".$info['smsto']); }}Copy the code


Application three: web crawling, cyclic execution of tasks, divided into multiple processes

Curl curl curl curl curl curl curl curl curl curl curl curl curl curl If we capture the 10 web pages through the for loop, it will take 20 seconds. Using multi-process, we can divide the task into 5 parts, which are respectively executed by 5 processes. Each process captures 2 urls and executes them concurrently, which takes 4s in total and improves the efficiency by 5 times.

<? php$url_arr = array();
for ($i= 0;$iThe < 10;$i{+ +)$url_arr[] = "www.baidu.com?wd=".$i;
}
echo "start:".date("Y-m-d H:i:s").PHP_EOL;
$workers = array();
for ($i= 0;$i< 5;$i{+ +)$process = new swoole_process('getContents'.true);
    $process->start();
    $process->write($i);
    $workers[] = $process; } // foreach ($workers as $process) {echo $process->read(a);echo PHP_EOL;
}
echo "end:".date("Y-m-d H:i:s").PHP_EOL;
function getContents(swoole_process $worker) {$i = $worker->read(a); global$url_arr;
    $res1 = execCurl($url_arr[($i* 2)]);$res2 = execCurl($url_arr[($i* 2 + 1));echo $res1.PHP_EOL.$res2;
}
function execCurl($url){
    sleep(2);
    return "handle ".$url." finished";
}
Copy the code

The execution effect is as follows:

Swoole’s multi-process method is much more convenient than curl’s method for simulating concurrency. So AGAIN, I suggest you learn swoole.