PHP asynchronous execution method, simulation of multithreading application Analysis _php Example

Source: Internet
Author: User
Tags sleep
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:
Copy Code code 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, every time we publish a new log, we need to send a mail notification to all the 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:
Copy Code code as follows:

<?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 ();
Echo time (). ' <br> ';

sendmail.php, execution takes 10 seconds:
Copy Code code as follows:

<?php
Sleep (10);
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.