PHP asynchronous execution method, multi-thread simulation _ PHP Tutorial

Source: Internet
Author: User
PHP asynchronous execution method, simulating multithreading. PHP asynchronous execution method, simulating multithreading PHP itself does not have multithreading, but it can generate the same effect by means of curve, such as multi-process method to achieve asynchronous calling, PHP asynchronous execution method only, simulating multithreading

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:

 /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 ---> notify the writer of publishing success ---> 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:

 /N ";} sleep (1); fputs ($ fp," GET/sendmail. php? Param = 1/r/n "); # the requested resource URL must be written to fclose ($ fp);} echo time ().'
'; Echo 'Call asyn_sendmail
'; Asyn_sendmail (); echo time ().'
';

Sendmail. php takes 10 seconds to execute:

 

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

1272472697

Call asyn_sendmail

1272472698

And generate a 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.

Articles you may be interested in
  • Php asynchronous debugging and online debugging of website programs
  • PHP Curl batch multi-threaded open url class
  • Javascript asynchronous loading improves the browsing speed of web pages
  • PHP uses CURL_MULTI to implement multithreading
  • Php obtains all the files in the directory and saves the results to the array program.
  • PHP uses Curl Functions to Capture webpages and download files with multiple threads
  • Php uses curl to implement multi-threaded classes and php curl to download images with multiple threads
  • How does php convert the br line breaks in html to line breaks in text input?

Http://www.bkjia.com/PHPjc/892342.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/892342.htmlTechArticlePHP asynchronous execution method, simulation of multi-threaded PHP itself does not have multi-threaded things, but can be a curve to create the same effect, such as multi-process method to achieve Asynchronous call, only...

Related Article

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.