Home  >  Article  >  Backend Development  >  PHP asynchronous execution method, simulated multi-threaded application analysis_PHP tutorial

PHP asynchronous execution method, simulated multi-threaded application analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:09:44747browse

PHP itself does not have multi-threading, but it can create the same effect in a curved way, such as using multiple processes to achieve asynchronous calls, which is 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:

Copy code The code is as follows:

$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 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 you press in the usual way:
After writing the log-> Click the submit button-> Insert the log into the database-> Send an email notification-> Inform the writer that the publication is successful
Then the author clicks the submit button until he sees success There may be a long wait between prompts, which is basically the process of 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 the log is written-> Click the submit button-> The log is inserted into the database---> Notify the writer of successful publication
└ Send email notification-> [Write down the log]
Write an actual program to test it. There are two files, namely write.php and sendmail.php. Use sleep(seconds in sendmail.php ) to simulate the time it takes for program execution.
write.php, execution takes 1 second:
Copy code The code is as follows:

function asyn_sendmail()
{
$fp = fsockopen('localhost',80,&$errno,&$errstr,5);
if(!$fp)
{ Echo "$ ERRSTR ($ Erno) & LT; br /& gt; /n";
}
Sleep (1);
FPUTS ($ fp, "get /sendmail. php?param=1/r/n"); #The requested resource URL must be written correctly
fclose($fp);
}
echo time().'
';
echo 'call asyn_sendmail
';
asyn_sendmail();
echo time().'
';


sendmail.php, execute It took 10 seconds:
Copy the code The code is as follows:
sleep(10 );
fopen("C:/" . time(), "w");


Access write.php through the page, the page output:
1272472697
call asyn_sendmail
1272472698
and generate the file in C:/:
1272472708
From the above results, we can see that sendmail.php takes at least 10 seconds, but does not block write.php to continue execution, indicating that this process is asynchronous.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327227.htmlTechArticlePHP itself does not have multi-threading, but it can create the same effect in a curved way, such as multi-process The method to achieve asynchronous calling is limited to command mode. There is also a more...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn