How to execute a large PHP Script?

前端 未结 7 1382
你的背包
你的背包 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:43

    If your host lets you, cron jobs are the best solution. A cron job is basically a normal php script that is automatically run by the web server at a specific time interval. For your needs I would create a script that runs every 5 mins and processes your numbers in batches of 100 (obviously you'll want to tweak the time interval and batch size to suit). This will keep your server load down and prevent you getting in trouble with your hosting provider for hogging resources.

    In order to track which batch your script should be processing, I would setup a track_batch table. These columns should give you a good indication of how to approach the problem:

    id, date_run, start_record, end_record, final_run

    Essentially:

    • Check to see the date of the last batch run. If it isn't the current date (or whatever other identifier you choose to use) for the current batch, then proceed.
    • If the last batch run was for the current date, then check the final_run column to see whether you've already finished processing all the numbers.
    • If you still have numbers to process, use the start and end records in conjunction with MySQL's LIMIT to build the db query that your script will use to get the next batch.
    • Process your numbers.
    • Store all the info from this batch in the track_batch table.
    • If the amount of numbers the query returns is ever less than the maximum batch size, you've reached the end and can set the final_run column to 1.

    Once you've got your script, you'll need to setup the cron job itself. Shared hosts are likely to have their own custom interfaces for doing this, so they are probably the best people to ask once you've got your script working.

    0 讨论(0)
提交回复
热议问题