PHP asynchronous implementation method to simulate Multithreading (Repost)

Source: Internet
Author: User
PHP asynchronous execution method, multi-thread simulation (reprinted article) Source: hi.baidu.comaqw518blogitem1e5cd4?ec52244f919b840.html PHP itself does not have multithreading, but it can produce the same effect by means of curves, for example, multi-process method to achieve asynchronous calling is limited to command mode. There is also a simpler way to use PHP asynchronous execution in Web programs to simulate multithreading (Repost)
Source:
Http://hi.baidu.com/aqw518/blog/item/1e5cd4116ec52244f919b840.html

PHP itself does not have multithreading, but it can produce the same effect by means of curves. for example, the asynchronous calling of multi-process mode is limited to the command mode.

In addition, a simpler method can be used in Web programs, that is, to request a URL using fsockopen () and fputs () without waiting for a response, if you do something in the requested page (URL), it is equivalent to asynchronous.

The key 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 does not matter after sending the request to the page another_page.php, and does not need to wait for the response data of the request page. with this, you can do something asynchronously in the requested page another_page.php.

For example, for a very practical application, every time we post a new log, we need to send an email notification to all the subscribers of this log. if we follow the common method:

Log writing-> click the submit button-> Insert the log to the database-> Send email notification-> inform the writer of the release success

Then, the author may wait for a long time between clicking the submit button and seeing the success prompt. it is basically waiting for the Mail sending process, such as an exception in the email service connection, slow server, or too many subscribers. In fact, no matter whether the mail is sent successfully or not, it is basically acceptable to ensure that the log is successfully saved. Therefore, the process of waiting for the Mail to be sent is very economic. this process can be executed asynchronously, in addition, the Mail sending results are not very concerned or recorded as logs for future reference.

The improved process is:

Log writing-> click the submit button-> Insert the log to the database ---> inform the writer that the publishing is successful.

└ 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 execution time of the program.

Write. php, execution takes 1 second:


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 "); # the requested resource URL must be written

Fclose ($ fp );
}

Echo time ().'
';

Echo 'Call asyn_sendmail
';

Asyn_sendmail ();

Echo time ().'
';

Sendmail. php takes 10 seconds to execute:


Sleep (10 );

Fopen ("C: \". time (), "w ");

Access write. php through the page, and the page output is:

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 and continues to execute, indicating that this 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.