doubi8512 2014-04-19 04:03 采纳率: 0%
浏览 26
已采纳

增强PHP脚本性能

(subject of this ques might not match with the ques, but i couldn't think of better) I have a webpage, where user provides email address of recipients, there can be 100 and more email addresses delimited by ; provided.in the textarea. Ofcourse i have to send an email to all those addresses. I have 2 approches in mind but couldn't decide on which one would provide better user experience and performance.
approach 1: i loop through all those emails in my js and send ajax request to php script. But then there would be 100 requests to the server, and if user closes browser in between, all email address wont go through

approach 2: i send all the 100 email addresses in one go to the php script, and let php script loop through emails. I am assuming that i would be able to echo some mesg back to client with success message after each loop count, and even if client is dead, then also at least php will keep executing untill loop ends

can somebody pls provide me cons and pros of these 2 approaches

  • 写回答

1条回答 默认 最新

  • douzhong1907 2014-04-19 04:19
    关注

    Here is an idea on how to implement a queue.

    define('MAX_EMAIL_BUFFER_SIZE', 15);
    
    // do a query to see how many emails are needed to be sent, you need to do store
    // this data in mysql or some other place.
    // array getEmails() { }
    
    $total = count( getEmails());
    $pages = ceil($total / MAX_EMAIL_BUFFER_SIZE);
    $i = 0;
    for(; $i < $total; $i++) {
      $offset = ($page - 1)  * MAX_EMAIL_BUFFER_SIZE;
        /* query
        SELECT
            *
        FROM
            table
        ORDER BY
            name
        LIMIT
            MAX_EMAIL_BUFFER_SIZE
        OFFSET
            $offset
       */
      // the result returned by the query are the emails you wills send.
      // do the above query in a function that returns the results
    
      foreach($data as $email) {
        mail(...);
      }
      // sleep for 10 seconds.
      sleep(10);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入