Implement asynchronous calling of multi-thread program code in PHP

Source: Internet
Author: User
Tags email account
This article introduces in detail how to implement asynchronous calling of multi-threaded methods in PHP. below we will send a recommendation email to 1000 users, the user entered or imported the email account and submitted it to the server for sending. for example, in this scenario, send an email to 1000 users... this article introduces in detail how to implement asynchronous calling of multi-threaded methods in PHP. below we will send a recommendation email to 1000 users, the user entered or imported the email account and submitted it to the server for sending.

For example, in the current scenario, a recommendation email is sent to 1000 users. the user enters or imports the email account and submits it to the server for sending. the code is as follows:

 

The user experience of this code is very poor and cannot be used in practice. first, sending so many emails will result in server operation timeout. In fact, a long waiting time will cause the user to doubt and lose confidence in the system products, however, you do not need to wait until all 1000 emails are sent before submitting the email. you can submit the email to the backend and then send the message to the user.

At this time, we need the "asynchronous execution" technology to execute code. asynchronous execution is characterized by silent execution in the background, and users do not have to wait for the code execution results. The advantages of asynchronous execution are as follows.

1. get rid of the application's dependency on a single task

2. improved program execution efficiency

3. improved program scalability

4. improved user experience in certain scenarios

5. because PHP does not support multithreading, the asynchronous calling of multiple HTTP requests achieves the parallel execution of the program. However, if there are too many HTTP requests, the system overhead is greatly increased.

User Experience: User waiting-> sending completed

Friends will ask, why is there a lack of email sending?

OK. when the user submits a request, the Mail sending task is transferred to a php program that handles the Mail separately, when the user sees that "sending is complete", the real message has not been sent. at this time, the sender program is working hard in the background and sending one letter to the outside.

The sendmail. php code is as follows:

 

System_mail.php

 

After changing to the asynchronous mode, the user submits the information and can immediately get the result "sent". The mail will be sent in the background one by one until the sending is complete.

After testing, several methods are summarized to share with you.

1. the simplest method is to embed an AJAX call in the HTML code returned to the client, or embed an img tag. src points to the time-consuming script to be executed.

This method is the simplest and fastest, and the server does not need to make any calls.

However, the disadvantage is that Ajax should be triggered after onLoad. that is to say, when the user clicks the page and closes it, the background script will not be triggered.

Using the img tag, this method cannot be called a strictly asynchronous execution. the user's browser will wait for the php script to be executed for a long time, that is, the status bar of the user's browser is still displayed.

Of course, you can also use other similar methods, such as script labels.

2. popen ()

Resource popen (string command, string mode );

Open an Pipeline pointing to a process. The process is generated by the execution of the derived command. open a pipeline pointing to the process, which is generated by the execution of the derived command.

Therefore, you can call it but ignore its output. the code is as follows:

Pclose (popen ("/home/xinchen/backend. php &", 'r '));

This method avoids the disadvantages of the first method and is also very fast, but the problem is that this method cannot request another WebService through the HTTP protocol and can only execute local script files, in addition, it can only be opened in one way, and a large number of parameters cannot be used to call scripts.

In addition, when traffic is high, a large number of processes will be generated. if external resources are used, you must consider competition on your own.

3. use CURL

In this method, set CUROPT_TIMEOUT to 1 (minimum: 1, depressing ). That is to say, the client must wait at least 1 second, and the code is as follows:

$ch = curl_init(); $curl_opt = array(CURLOPT_URL, 'http://www.example.com/backend.php', CURLOPT_RETURNTRANSFER, 1, CURLOPT_TIMEOUT, 1,);  curl_setopt_array($ch, $curl_opt); curl_exec($ch); curl_close($ch);

4. use fsockopen

This method should be perfect, but the disadvantage is that you need to spell out the HTTP header by yourself. the code is as follows:

 \ N ";} else {$ out =" GET/backend. php/HTTP/1.1 \ r \ n "; $ out. = "Host: www.example.com \ r \ n"; $ out. = "Connection: Close \ r \ n"; fwrite ($ fp, $ out);/* ignore the execution result while (! Feof ($ fp) {echo fgets ($ fp, 128) ;}*/fclose ($ fp) ;}?>


Address:

Reprinted at will, but please attach the article address :-)

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.