Simple PHP multi-threading solution
When we are doing projects, we have some needs, especially data The response processing takes a lot of time. Since PHP is a short-life cycle scripting language, after the default 30 seconds, PHP's data processing has not been completed, and PHP's life cycle is over.
At this time, you need to use an asynchronous concurrent processing strategy, that is to say, multiple requests that can be issued in one PHP call. These requests are not executed in sequence, but can be executed asynchronously and concurrently. Some requests are used in the background Process data, some requests are used to accept background response status, and do some simple interactions with users based on the status.
But here comes the problem. We all know that PHP itself does not support multi-threading, so how should we implement multi-threading in PHP?
1. Three methods of php simulation to achieve multi-threading
1. PHP multi-threading under Linux
The following things are the source Since PHP's pcntl_fork function. Because this function depends on the implementation of operating system fork, the things discussed in this article only apply to Linux/Unix. So let’s take a look at the usage of this function first. The PHP manual says this:
Create a child process through pcntl_fork. If the return value is -1, then the creation of the child process failed. The creation was successful. The process ID will be returned to the parent process, and 0 will be returned to the child process. It is difficult to understand, so it should be written like this:
This change will make it easier to understand, if your parent process wants to know that the child process exits normally. , you can add the previous pcntl_wait.
2. Through stream_socket_client method
function sendStream() { $english_format_number = number_format($number, 4, '.', ''); echo $english_format_number; exit(); $timeout = 10; $result = array(); $sockets = array(); $convenient_read_block = 8192; $host = "test.local.com"; $sql = "select waybill_id,order_id from xm_waybill where status>40 order by update_time desc limit 1 "; $data = Yii::app()->db->createCommand($sql)->queryAll(); $id = 0; foreach ($data as $k => $v) { if ($k % 2 == 0) { $send_data[$k]['body'] = NoticeOrder::getSendData($v['waybill_id']); } else { $send_data[$k]['body'] = array($v['order_id'] => array('extra' => 16)); } $data = json_encode($send_data[$k]['body']); $s = stream_socket_client($host . ":80", $errno, $errstr, $timeout, STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT); if ($s) { $sockets[$id++] = $s; $http_message = "GET /php/test.php?data=" . $data . " HTTP/1.0 Host:" . $host . " "; fwrite($s, $http_message); } else { echo "Stream " . $id . " failed to open correctly."; } } while (count($sockets)) { $read = $sockets; stream_select($read, $w = null, $e = null, $timeout); if (count($read)) { /* stream_select generally shuffles $read, so we need to compute from which socket(s) we're reading. */ foreach ($read as $r) { $id = array_search($r, $sockets); $data = fread($r, $convenient_read_block); if (strlen($data) == 0) { echo "Stream " . $id . " closes at " . date('h:i:s') . ".
"; fclose($r); unset($sockets[$id]); } else { $result[$id] = $data; } } } else { /* A time-out means that *all* streams have failed to receive a response. */ echo "Time-out! "; break; } } print_r($result); }
3. Replace multi-threading through multi-process
function daemon($func_name,$args,$number){ while(true){ $pid=pcntl_fork(); if($pid==-1){ echo "fork process fail"; exit(); }elseif($pid){//创建的子进程 static $num=0; $num++; if($num>=$number){ //当进程数量达到一定数量时候,就对子进程进行回收。 pcntl_wait($status); $num--; } }else{ //为0 则代表是子进程创建的,则直接进入工作状态 if(function_exists($func_name)){ while (true) { $ppid=posix_getpid(); var_dump($ppid); call_user_func_array($func_name,$args); sleep(2); } }else{ echo "function is not exists"; } exit(); } } } function worker($args){ //do something } daemon('worker',array(1),2);
2. The real way to realize multi-threading in php
The real way to realize multi-threading in php can be done by installing the php extension pthread.
Click here to download https://github.com/krakjoe/pthreads But this download is version 3, which can only be used with php 7. We need to use version 2
Then refresh the page as follows, drag to the bottom:
Find version 2# on the next page
##Download, this v2 can only be used by php5 Download and install: Or, you can download directly like this:cd /tools wget https://github.com/krakjoe/pthreads/archive/v2.0.10.zip unzip v2.0.10.zip cd pthreads-2.0.10 /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make install
vim /etc/php.ini 添加 extension=pthreads.so
php tutorial"
The above is the detailed content of Simple php multi-threading solution. For more information, please follow other related articles on the PHP Chinese website!