There is only one communication between the browser and the server for the non-connected HTTP protocol, the non-connected program is characterized by the client Request server, the service side output the corresponding program according to the request, cannot maintain the persistent connection.
This is a problem, a client's corresponding server may execute 1 seconds or 1 minutes, so that the browser will always be waiting, if the program is slow to execute, the user may not be patient to shut down the browser.
And sometimes we do not need to care about the results of the program implementation, there is no need to waste time and patience to wait, then we have to figure out a way to let the program wait in the background silently.
For example, there is a scene, to send 1000 users a referral mail, the user input or import mail account to submit the server to perform the send.
{
SendMail (...); /Send mail}?>
This Code user experience is very poor, and can not actually use, the first to send so many messages will result in server run time-out, in fact, long user wait time will let users doubt and loss of confidence in the system products. However, the user does not have to wait until 1000 messages have been sent complete before the submission is successful, we can submit the background directly to the user prompt to send the success, and then let the background program silently sent.
This time we need "asynchronous execution" technology to execute code, asynchronous execution is characterized by the background of silent execution, the user does not have to wait for the result of code execution, using the benefits of asynchronous execution:
1. Get rid of the application's dependency on a single task
2. Improved execution efficiency of the program
3. Improve the extensibility of the program
4. Improved user experience in a certain scenario
5. Because PHP does not support multi-threading, the use of asynchronous calls to request more than one HTTP way to achieve the program parallel execution effect, but note that the request of excessive HTTP, will greatly increase the system overhead
Common ways to execute PHP asynchronously:
1. Client page with Ajax technology request server
1. The simplest approach is to embed an AJAX call in the HTML code returned to the client, or embed an IMG tag, which points to the time-consuming script to execute.
This method is the simplest and fastest. The server side does not have to make any calls.
However, the disadvantage is that, in general, Ajax should be triggered after the onload, that is, the user points to open the page, then close, it will not trigger our background script.
In the case of an IMG tag, this approach cannot be called a strict asynchronous execution. The user's browser waits for the execution of the PHP script to complete, that is, the status bar of the user's browser is always displayed in load.
Of course, other similar principles can be used, such as script tags and so on.
2.popen () function
Resource Popen (String command, string mode);
Opens a pipeline that points to a process that is generated by the execution of a derived command command. Opens a pipeline that points to a process that is generated by the execution of a derived command command.
So you can call it, but ignore its output.
?
Pclose (Popen ("/home/xinchen/backend.php &", ' R '));
This method avoids the disadvantage of the first method and is also very fast. However, the problem is that this method cannot request another webservice through the HTTP protocol, only local script files can be executed. And can only be opened one way, unable to wear a large number of parameters to the called script.
And if, when the traffic is very high, there will be a lot of process. If you use external resources, consider the competition yourself.
3.CURL expansion
Curl is a powerful HTTP command-line tool that simulates HTTP requests such as Post/get, and then gets and extracts data that appears on the "standard output" (stdout)
$ch = Curl_init ();
$curl _opt = Array (curlopt_url, ' http://www.example.com/backend.php ',
Curlopt_returntransfer, 1,
Curlopt_timeout, 1,);
Curl_setopt_array ($ch, $curl _opt);
Curl_exec ($ch);
Curl_close ($ch);
Use Curl to set the Curopt_timeout to 1 (minimum is 1, depressed). In other words, the client must wait at least 1 seconds.
4.fscokopen () function
Fsockopen is a very powerful function, support socket programming, you can use Fsockopen to implement the socket program such as mail sending, etc., using fcockopen need to manually stitching out the header section
Official Document: http://cn.php.net/fsockopen/
?
$fp = Fsockopen ("www.2cto.com", $errno, $errstr, 30);
if (! $fp) {
echo "$errstr ($errno)
\ n ";
} else {
$out = "get/backend.php/http/1.1\r\n";
$out. = "host:www.2cto.com\r\n";
$out. = "connection:close\r\n\r\n";
Fwrite ($fp, $out);
/* Ignore execution results
while (!feof ($fp)) {
Echo fgets ($FP, 128);
}*/
Fclose ($FP);
}
Excerpted from Qeenoo
http://www.bkjia.com/PHPjc/478345.html www.bkjia.com true http://www.bkjia.com/PHPjc/478345.html techarticle There is only one communication between the browser and the server for the non-connected HTTP protocol, the non-connected program is characterized by the client requesting the service side, the service side output the corresponding process according to the request ...