Processing large amounts of data in PHP without a browser timeout

前端 未结 4 771
谎友^
谎友^ 2020-12-23 15:12

I have array of mobile numbers, around 50,000. I\'m trying to process and send bulk SMS to these numbers using third-party API, but the browser will freeze for some minutes

4条回答
  •  庸人自扰
    2020-12-23 16:12

    I'm assuming these numbers are in a database, if so you should add a new column titled isSent (or whatever you fancy).

    This next paragraph you typed should be queued and possibly done night/weekly/whenever appropriate. Unless you have a specific reason too, it shouldn't be done in bulk on demand. You can even add a column to the db to see when it was last checked so that if a number hasn't been checked in at least X days then you can perform a check on that number on demand.

    Processing of the data involves checking mobile number type (e.g CDMA), assigning unique ids to all the numbers for further referencing, check for network/country unique charges, etc.

    But that still leads you back to the same question of how to do this for 50,000 numbers at once. Since you mentioned cron jobs, I'm assuming you have SSH access to your server which means you don't need a browser. These cron jobs can be executed via the command line as such:

    /usr/bin/php /home/username/example.com/myscript.php

    My recommendation is to process 1,000 numbers at a time every 10 minutes via cron and to time how long this takes, then save it to a DB. Since you're using a cron job, it doesn't seem like these are time-sensitive SMS messages so they can be spread out. Once you know how long it took for this script to run 50 times (50*1000 = 50k) then you can update your cron job to run more/less frequently.

    $time_start = microtime(true);
    set_time_limit(0);
    
    function doSendSMS($phoneNum, $msg, $blah);
    
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    saveTimeRequiredToSendMessagesInDB($time);
    

    Also, you might have noticed a set_time_limit(0), this will tell PHP to not timeout after the default 30seconds. If you are able to modify the PHP.ini file then you don't need to enter this line of code. Even if you are able to edit the PHP.ini file, I would still recommend not changing this feature since you might want other pages to time out.

    http://php.net/manual/en/function.set-time-limit.php

提交回复
热议问题