Home >PHP Framework >Swoole >Take a look at swoole multi-process operation

Take a look at swoole multi-process operation

coldplay.xixi
coldplay.xixiforward
2021-03-03 10:50:022473browse

Take a look at swoole multi-process operation

Execute multiple tasks simultaneously

Convert sequentially executed tasks into parallel execution (task Logically it can be executed in parallel)

For example, we need to judge the known user data whether emails and text messages need to be sent, and if so, send them.

When multi-process is not used, we first determine whether to send an email and send it if necessary; then we determine whether a text message needs to be sent and send it if necessary. If it takes 2 seconds to send an email and 2 seconds to send a text message, then it will take about 4 seconds for us to complete the task.

If we use multi-threading, we can open two threads, one for processing emails and one for processing text messages. It will take about 2 seconds in total, and the processing time is shortened by half.

Recommendation (free): swoole

<?php/**
 * Created by PhpStorm.
 * User: zhezhao
 * Date: 2016/10/20
 * Time: 10:37
 */$info = array(    "sendmail"=>1,    "mailto"=>"12345@qq.com",    "sendsms"=>1,    "smsto"=>"123456");echo "start:".date("Y-m-d H:i:s").PHP_EOL;$mail_process = new swoole_process(&#39;sendMail&#39;,true);$mail_process->start();$sms_process = new swoole_process(&#39;sendSMS&#39;,true);$sms_process->start();//主进程输出子进程范围内容echo $mail_process->read();echo PHP_EOL;echo $sms_process->read();echo PHP_EOL;echo "end:".date("Y-m-d H:i:s").PHP_EOL;//并行函数function sendMail(swoole_process $worker){
    global $info;    if($info[&#39;sendmail&#39;]==1){
        sleep(2);        $worker->write("send mail to ".$info[&#39;mailto&#39;]);
    }
}function sendSMS(swoole_process $worker){
    global $info;    if($info[&#39;sendmail&#39;]==1){
        sleep(2);        $worker->write("send sms to ".$info[&#39;smsto&#39;]);
    }
}

Take a look at swoole multi-process operation

Divide large tasks into multiple small ones Task

Divide the cyclically executed tasks into multiple processes for execution to improve work efficiency

Suppose we now have a system that grabs web content through curl According to the requirement, 10 web pages need to be crawled. The URL address is read through the array. Each curl takes 2 seconds. If we crawl these 10 web pages through a for loop, it will take 20 seconds. Using multi-process, we can divide the task into 5 parts and execute them respectively by 5 processes. Each process grabs 2 URLs and executes them concurrently. A total of It takes 4 seconds and the efficiency is increased by 5 times.

<?php/**
 * Created by PhpStorm.
 * User: zhezhao
 * Date: 2016/10/20
 * Time: 10:51
 */$url_arr = array();for ($i=0;$i<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(&#39;getContents&#39;,true);    $process->start();    $process->write($i);    $workers[] = $process;
}//主进程数据结果foreach ($workers as $process){    echo $process->read();    echo PHP_EOL;
}echo "end:".date("Y-m-d H:i:s").PHP_EOL;function getContents(swoole_process $worker){
    $i = $worker->read();    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";
}

Take a look at swoole multi-process operation

Summary

Both of the above situations are essentially combining tasks that have no logical sequence into one. Processes are executed concurrently to improve efficiency.

The PHP mechanism itself does not provide multi-threaded operations. The ptcl extension provides an interface for PHP to operate Linux multi-processes.

Personally, I feel that swoole's multi-process process method is more convenient.

The above is the detailed content of Take a look at swoole multi-process operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete