Php asynchronous calls improve user experience. this is written by a friend of mine who has a good technology, I want to post on my blog so that php fans and friends who are interested in php learning can refer .??????? Case: In an SNS community in PHP, the user sends an email to his friends (hundreds of friends) in the system. the content of each email is different. after the email is sent, the prompt is sent! PHP Asynchronous call in common php to improve user experience
This is written by a good technical friend of mine. if you want to post it on my blog, you can get reference from php fans and friends who are interested in php learning.
??????? Case: PHP language
In an SNS community, users send emails to their friends (hundreds of friends) in the system. the content of each email is different. after sending the email, the system prompts that the email has been sent!
Common PHP writing
Sendmail. php
?
Assume that 100 emails are sent. What are the results of this operation?
User Experience: User waiting-> sending dozens of emails-> System timeout error message returned
This operation requires sending a large number of emails, resulting in php execution time being too long, and users are excited to wait. If apache or nginx waits for more than the allowed execution time, a timeout error is returned. At this time, the user does not know whether the operation was successful or not, and how many emails have been sent.
We can see that the user experience of this code is very poor and the task cannot be completed smoothly.
What should I do?
Here we mention the concept of asynchronous execution.
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.
Sendmail. php
System_mail.php
? After changing to the asynchronous mode, the user submits the information and can immediately get the result "sent ". The email will be sent in the background one by one until the email is sent.
This article from php enthusiasts http://www.phplover.cn/, the original address: http://www.phplover.cn/post/phpyibugongzuotishengyonghutiyan.html
?