PHP is a process-oriented language from a mainstream perspective. Its biggest disadvantage is that it cannot achieve multi-thread management. The execution of its program is from beginning to end, and it is executed according to logic all the way. Branches are impossible. This is One of the reasons that restricts PHP from developing into a higher-level language among mainstream programming languages.
In PHP, sometimes we actually want to perform another operation at the same time when performing a certain operation. Take a scenario: when users are grabbing tickets, you do not want users to queue up to connect to the database for query. Judge, insert, and then return the user results after completion. In fact, we don’t need the user to wait that long. After the user submits, we can directly tell him that he has successfully grabbed the ticket. As for the various operations, we can just leave it to the background for processing. Of course, we now use message lists to handle this situation. We store each request submitted by the user in a message queue to tell the user that it has been completed. After the user happily closes the page, in fact, the background is still receiving messages one by one. Remove the request from the queue for operation. Our article uses a heterogeneous approach to enable operations to run in the background without the user having to wait.
First, we need to create a request entry:
Submitted data
Submit to the background
Tell the user that it has been done
Secondly, we need a background handler to check whether the user is online It does not affect its operation:
<?php ignore_user_abort(true); set_time_limit(0);
Incoming data
Data processing
Now the question is, in the first piece of code, how to "submit to the background"? We implement this function through a non-blocking request. That is to create a url that can be accessed, run the second program on this url, and request the url through a request, thus activating the automatic running of the second program.
Next let’s look at the code directly:
// 远程请求(不获取内容)函数 function _sock($url) { $host = parse_url($url,PHP_URL_HOST); $port = parse_url($url,PHP_URL_PORT); $port = $port ? $port : 80; $scheme = parse_url($url,PHP_URL_SCHEME); $path = parse_url($url,PHP_URL_PATH); $query = parse_url($url,PHP_URL_QUERY); if($query) $path .= '?'.$query; if($scheme == 'https') { $host = 'ssl://'.$host; } $fp = fsockopen($host,$port,$error_code,$error_msg,1); if(!$fp) { return array('error_code' => $error_code,'error_msg' => $error_msg); } else { stream_set_blocking($fp,true);//开启了手册上说的非阻塞模式 stream_set_timeout($fp,1);//设置超时 $header = "GET $path HTTP/1.1\r\n"; $header.="Host: $host\r\n"; $header.="Connection: close\r\n\r\n";//长连接关闭 fwrite($fp, $header); usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功 fclose($fp); return array('error_code' => 0); } }
We created a function based on fsockopen. In this function, fsockopen is used to access the url. However, during the access, it is not required to obtain the content displayed in the url, but only issues Access request, close this access immediately after the request arrives. The advantage of this is that there is no need to wait for the visited URL to return reliable information, which saves time. The execution time of this code is between 0.1-0.2 seconds, which is almost imperceptible to ordinary visitors. Therefore, when using it, you only need to call this function and the corresponding url. However, there is no data transmission part provided here. How to transmit data, in fact, you only need to add the post content to $header.
In addition to fsockopen, curl can actually achieve this effect. Some hosts do not support fsockopen, so we can use curl to achieve it.
function _curl($url) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_TIMEOUT,1); $result = curl_exec($ch); curl_close($ch); return $result; }
The key to this code is to provide a Timeout, only 1 second, which means that curl makes a request, regardless of whether the returned content is received, the access will be closed after 1 second, so the execution of this function The data is between 1.0-1.1 seconds. But for users, if it is an application that requires data processing, the wait of 1 second is almost ignored. If you want to use a simpler and easier-to-understand code, you can choose curl to implement it.