Multiple tasks are executed simultaneously
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.
Here is an example of the code:
<? php /** * Created by PhpStorm. * User: cyw0413 * Date: 2018/10/20 * Time: 10:37 */ $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(); 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['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
Big tasks are broken down into smaller tasks
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.
Here is an example of the code
<? php /** * Created by PhpStorm. * User: cyw0413 * Date: 2018/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('getContents',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"; }Copy the code
conclusion
In both cases, logically unrelated tasks are executed concurrently by multiple processes to improve efficiency.
The PHP mechanism itself does not provide multithreading; the PTCL extension provides an interface for PHP to manipulate Linux multiprocesses.
Pay attention and don’t get lost
All right, everybody, that’s all for this article. All the people here are talented. As I said before, there are many technical points in PHP, because there are too many, it is really difficult to write, you will not read too much after writing, so I have compiled it into PDF and document, if necessary
Click on the code: PHP+ “platform”
As long as you can guarantee your salary to rise a step (constantly updated)
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc