PHP itself does not have multiple threads, but it can be curved to create the same effect, such as a multiple-process approach to asynchronous invocation, limited to command mode.
There is also a simpler way to use the WEB program, which is to request a URL with Fsockopen (), fputs (), without waiting for a return, if you do something in the requested page (URL) that is asynchronous.
The key code is as follows:
<?php
$fp =fsockopen (' localhost ',80,& $errno,& $errstr, 5);
if (! $fp) {
echo $errstr ($errno) <br/>/n ";
}
Fputs ($fp, "Get another_page.php?flag=1/r/n");
Fclose ($FP);
The above code sends the request to the page another_page.php to ignore, does not need to wait for the request page The response data, can use this point to be able to do something asynchronously in the requested page another_page.php.
For example, a very practical application, each time we publish a new log, we need to send an email notification to all subscribers to the log, if the usual way is:
Log write-> point submit button-> log Insert to database-> send mail notification-> inform the writer to publish success
Then the author may wait for a very long time between the Point submit button and the success note, basically in the process of waiting for the mail to be sent, for example, the connection mail service is abnormal, or the device is slow or the subscriber is too many. In fact, regardless of whether the mail is successful or not, to ensure the success of the log is basically acceptable, so the process of waiting for the message to send is very economical, this process can be performed asynchronously, and the results of the mail sent less concerned about or recorded in log form.
The improved process is:
Log-> Point submit button-> log insert to database---> Tell authors to publish successfully---> Send mail notification-> [note log]
Write a practical program to test, there are two files, respectively, write.php and sendmail.php, in sendmail.php with sleep (seconds) to simulate the use of program execution time.
write.php, execution takes 1 seconds:
<?php
function Asyn_sendmail () {
$fp =fsockopen (' localhost ',80,& $errno,& $errstr, 5);
if (! $fp) {
echo $errstr ($errno) <br/>/n ";
}
Sleep (1);
Fputs ($fp, "get/sendmail.php?param=1/r/n"); #请求的资源 URL must be written to
fclose ($FP);
}
Echo time (). ' <br> ';
Echo ' Call asyn_sendmail<br> ';
Asyn_sendmail ();
sendmail.php, execution takes 10 seconds:
<?php Sleep
(ten);
fopen ("c:/". Time (), "w");
Through the page Access write.php, page output:
1272472697
Call Asyn_sendmail
1272472698
And in c:/ To generate a file:
1272472708
From the results above, it can be seen that sendmail.php takes at least 10 seconds, but does not block write.php from continuing down, indicating that the process is asynchronous.
Articles that you may be interested in
- The method of PHP asynchronous debugging and online debugging Web site program
- Javascript asynchronous loading to improve browsing speed of web pages
- PHP Curl Batch multi-threaded open URL class
- PHP Determines whether server SSL is enabled, which is to determine whether HTTPS connections are supported
- PHP uses Curl functions to implement multi-threaded crawl Web pages and download files
- A program in which PHP gets all the files in the directory and saves the results to an array
- PHP using curl to implement multithreaded classes, PHP curl multi-Threaded download pictures
- PHP uses Curl_multi to implement multithreading