How to execute a large PHP Script?

前端 未结 7 1477
你的背包
你的背包 2020-12-24 08:16

Well basically I may want to execute a script that may take as much as 1 hours as well.

What I really want to do is Send SMS to my users using a third party API. So

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 08:34

    It's not the best options to use set_time_limit(0), because that'd means it'll run indefinitely even if you have a bug and your script enters an infinite loop.

    Instead, if you estimate each SMS is going to take 5 seconds, use this approach:

    while( $there_are_more_sms_to_be_sent ){
      set_time_limit(30); // enough spare time, just in case.
    
      // Do your sending, blah blah
    }
    

    That way, the time limit will be sequentially updated to 30 seconds. Of course you might have the infinite loop problem with that single while, but if you have other calls inside that while that limit will prevent those calls to be to blame.

提交回复
热议问题