Home > Article > Backend Development > PHP asynchronous execution method, simulate multi-threading_PHP tutorial
PHP itself does not have multi-threading, but it can create the same effect in a curved way, such as using multiple processes. Asynchronous calls are limited to command mode.
There is also a simpler way that can be used in Web programs, that is to use fsockopen(), fputs() to request a URL without waiting for the return. If you do it in the requested page (URL) These things are equivalent to 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 and then ignores it. There is no need to wait for the response data of the requested page. You can use this to do something asynchronously in the requested page another_page.php.
For example, in a very practical application, every time we publish a new blog, we need to send an email notification to all the subscribers of the blog. If we follow the usual method:
After writing the log -> Click the submit button -> Insert the log into the database -> Send an email notification -> Notify the writer of successful publication
Then the author may wait for a long time between clicking the submit button and seeing the success prompt. Basically, he is waiting for the email to be sent. For example, the connection to the email service is abnormal, the server is slow, or there are too many subscribers. In fact, regardless of whether the email is sent successfully or not, it is basically acceptable to ensure that the log is saved successfully, so the process of waiting for the email to be sent is very uneconomical. This process can be executed asynchronously, and the result of the email sending is not of concern or is based on the log. The form is recorded for future reference.
The improved process is:
After writing the log -> Click the submit button -> Insert the log into the database ---> Notify the author of successful publication ---> Send email notification -> [Write down the log]
Write an actual program to test it. There are two files, write.php and sendmail.php. Use sleep(seconds) in sendmail.php to simulate the time taken by the program execution.
write.php, execution takes 1 second:
<?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 一定要写对 fclose($fp); } echo time().'<br>'; echo 'call asyn_sendmail<br>'; asyn_sendmail(); echo time().'<br>';
sendmail.php, execution takes 10 seconds:
<?php sleep(10); fopen("C:/".time(),"w");
Access write.php through the page, page output:
1272472697
call asyn_sendmail
1272472698
And generate files in C:/:
1272472708
It can be seen from the above results that sendmail.php takes at least 10 seconds, but does not block write.php to continue execution, indicating that this process is asynchronous.