PHP asynchronous execution method, simulation multi-threaded application Analysis _php Tutorial

Source: Internet
Author: User
PHP itself does not have many threads, but it can be curved to produce the same effects, such as multi-process ways to reach asynchronous calls, only in command mode.
There is also a simpler way to use the WEB program, which is to use Fsockopen (), fputs () to request a URL without waiting for a return, if you do something in that requested page (URL) is equivalent to asynchronous.
The key code is as follows:
Copy CodeThe code is as follows:
$fp = Fsockopen (' localhost ',80,& $errno,& $errstr, 5);
if (! $fp)
{
echo "$errstr ($errno)
/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 does not have to wait for the response data of the requested page, so that it can do something asynchronously in the requested page another_page.php.
For example, a very practical application, we need to send an email notification to all subscribers to the log whenever a new journal is published, if the usual way is:
Post-write log insert to database--Send mail Notification--to notify authors of successful publishing
Then the author may wait a long time between the Point submit button and the success prompt, basically waiting for the message to be sent, such as connecting to the mail service exception, or slow or too many subscribers. In fact, regardless of the success of the message sent to ensure that the success of the log is basically acceptable, so the process of waiting for the message to be sent is not economical, the process can be executed asynchronously, and the results of the message sent less care or log records for the record.
The improved process is:
Post-write, submit button, log insert to database---> tell the composer to publish successfully
└ e-mail notification, [note the 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 execution of the program to use the time.
write.php, execution takes 1 seconds:
Copy CodeThe code is as follows:
function Asyn_sendmail ()
{
$fp = Fsockopen (' localhost ',80,& $errno,& $errstr, 5);
if (! $fp)
{
echo "$errstr ($errno)
/n ";
}
Sleep (1);
Fputs ($fp, "get/sendmail.php?param=1/r/n"); #请求的资源 URL must be written to
Fclose ($FP);
}
Echo time (). '
';
Echo ' Call Asyn_sendmail
';
Asyn_sendmail ();
Echo time (). '
';

sendmail.php, execution takes 10 seconds:
Copy CodeThe code is as follows:
Sleep (10);
fopen ("c:/"). Time (), "w");

Page access write.php, page output:
1272472697
Call Asyn_sendmail
1272472698
And in c:/ Generate File:
1272472708
From the above results, it can be seen that sendmail.php takes at least 10 seconds, but does not block write.php continues to execute, indicating that the process is asynchronous.

http://www.bkjia.com/PHPjc/327227.html www.bkjia.com true http://www.bkjia.com/PHPjc/327227.html techarticle PHP itself does not have many threads, but it can be curved to produce the same effects, such as multi-process ways to reach asynchronous calls, only in command mode. In addition, there is a more ...

  • 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.