如何执行大型PHP脚本? [英] How to execute a large PHP Script?

查看:63
本文介绍了如何执行大型PHP脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我可能想要执行一个可能要花费多达1个小时的脚本.

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

我真正想做的是使用第三方API向我的用户发送短信.因此,基本上就像我为脚本提供了一系列电话号码并触发了发送SMS的方法一样.

What I really want to do is Send SMS to my users using a third party API. So its basically like I supply my script with an array of phone numbers and fire the method to send SMS.

但是,假设发送1条短信需要5秒钟,而我想发送1000条SMS,大约需要1-2个小时.我不能使用set_time_limit(),因为我在共享主机上.

However assuming it take 5 seconds to send 1 SMS and I want to send 1000 SMS which is roughly 1 - 2 hours. I can't use set_time_limit() because I am on shared host.

一种方法是在会话中存储数字并执行每个SMS,然后使用javascript刷新该页面直至结束.这样,我需要保持浏览器处于打开状态,并且如果断开Internet连接,执行将停止.

One way to do this is store numbers in a session and execute each SMS and use javascript to refresh that page until end. This way I need to keep my browser open and the execution will stop if my Internet Connection is disconnected.

那么,还有什么更好的方法吗?

So, Is there any better way to do this ?

希望我足够清楚地解释我想要什么?我想执行一个大型脚本,该脚本可能要花几个小时才能执行,而不会超时.

Hope I am clear enough to explain what I want? I want to execute a large script that may take hours to execute without getting timeout.

推荐答案

如果您的主机允许您,则cron作业是最佳解决方案. cron作业基本上是普通的php脚本,由Web服务器在特定时间间隔自动运行.为了满足您的需求,我将创建一个脚本,该脚本每5分钟运行一次,并以100为批次处理您的数字(很显然,您需要调整时间间隔和批次大小以适应需要).这样可以减少服务器的负载,并防止您与托管服务提供商发生资源浪费问题.

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.

为了跟踪脚本应处理的批次,我将设置一个track_batch表.这些专栏应为您很好地说明如何解决此问题:

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

id, date_run, start_record, end_record, final_run

本质上:

  • 检查以查看最后一次的日期 批量运行.如果不是当前日期 (或您选择的其他任何标识符 用于当前批次),然后 继续.
  • 如果在当前日期最后一次运行 ,请检查 final_run列以查看是否 您已经完成处理 所有的数字.
  • 如果仍有数字需要处理,请使用开始和结束 与MySQL一起记录 LIMIT建立您的资料库查询 脚本将用于获取下一个 批.
  • 处理您的电话号码.
  • 将该批次的所有信息存储在track_batch表中.
  • 如果查询返回的数字数量小于最大数量 批次大小,您已经到了尽头 并可以将final_run列设置为 1.
  • 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.

一旦有了脚本,就需要设置cron作业本身.共享主机可能具有自己的自定义界面来执行此操作,因此它们可能是在脚本运行后最好询问的人.

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.

这篇关于如何执行大型PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆