如何执行大型PHP脚本?
基本上我可能想要执行一个可能需要长达 1 小时的脚本。
我真正想做的是使用第三方 API 向我的用户发送短信。所以基本上就像我为我的脚本提供一系列电话号码并触发发送短信的方法。
然而,假设发送 1 条短信需要 5 秒,而我想发送 1000 条短信,大约需要 1 - 2 小时。我无法使用 set_time_limit()
因为我位于共享主机上。
实现此目的的一种方法是将数字存储在会话中并执行每条短信并使用 JavaScript 刷新该页面直至结束。这样我需要保持浏览器打开,如果我的互联网连接断开,执行就会停止。
那么,有没有更好的方法来做到这一点?
希望我足够清楚地解释我想要什么?我想执行一个大型脚本,可能需要几个小时才能执行而不会超时。
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 its basically like I supply my script with an array of phone numbers and fire the method to send SMS.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从命令行或 shell 脚本、cron 作业等执行的 PHP 脚本没有超时。
对于 CLI 调用的脚本,即使您使用
set_time_limit()
动态设置 PHP 脚本的超时 函数,它没有任何作用。A PHP script executed from the command-line or from a shell script, cron job, etc. does not have a timeout.
For CLI-invoked scripts, even if you set the PHP script's timeout dynamically with the
set_time_limit()
function, it has no effect.从命令行运行的 PHP 脚本不受
max_execution_time
选项的影响。所以你完全不用担心。
PHP scripts running from the command line aren't affected by
max_execution_time
option.So you don't have to worry at all.
如果您的主机允许,那么 cron 作业是最好的解决方案。 cron 作业基本上是一个普通的 php 脚本,由 Web 服务器在特定的时间间隔自动运行。根据您的需求,我将创建一个每 5 分钟运行一次的脚本,并以 100 个为一组处理您的数字(显然您需要调整时间间隔和批量大小以适应)。这将降低您的服务器负载,并防止您因占用资源而与托管提供商发生麻烦。
为了跟踪您的脚本应该处理哪个批次,我将设置一个 track_batch 表。这些列应该为您提供如何解决问题的良好指示:
id、date_run、start_record、end_record、final_run
本质上:
批量运行。如果不是当前日期
(或您选择的任何其他标识符
使用)当前批次,然后
继续。
Final_run 列查看是否
你已经完成处理
所有的数字。
与 MySQL 结合的记录
LIMIT 构建您的数据库查询
脚本将用于获取下一个
批。
批量大小,您已到达终点
并可以将 Final_run 列设置为
1.
获得脚本后,您需要设置 cron 作业本身。共享主机可能有自己的自定义界面来执行此操作,因此一旦您的脚本正常运行,他们可能是最好的询问者。
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:
batch run. If it isn't the current date
(or whatever other identifier you choose to
use) for the current batch, then
proceed.
final_run column to see whether
you've already finished processing
all the numbers.
records in conjunction with MySQL's
LIMIT to build the db query that your
script will use to get the next
batch.
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.
使用
set_time_limit(0)
并不是最好的选择,因为这意味着即使您有错误并且脚本进入无限循环,它也会无限期地运行。相反,如果您估计每条短信将花费 5 秒,请使用此方法:
这样,时间限制将依次更新为 30 秒。当然,您可能会遇到单个
while
的无限循环问题,但如果您在该while
内有其他调用,则该限制将阻止这些调用应该受到指责。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:
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 thatwhile
that limit will prevent those calls to be to blame.在你可以运行 CRON 作业的情况下,
我通常有一个队列、一个管理器和工作人员。除非您可以一次调用短信 API,否则此模型可以帮助您,并且您不必担心超时,因为每个工作人员都会自行管理。
我有类似的东西:
send.php 将发送每条短信。现在我的运行速度为 300/分钟,因为这是您可以在 cron 作业上设置的最大频率
IN THE CASE YOU CAN RUN CRON JOBS
I usually have a queue, a manager and workers. Unless you can call the sms api once at the time this model can help you, and you souldn't worry about timeouts since each worker will manage it selves.
I have something like:
and send.php will send each sms. Right now I have this running at a rate of 300/minute since is the max frequency that you can setup on a cron job
您可以还是不能使用
set_time_limit()
?如果可以的话..使用它:
You can or you can't use
set_time_limit()
?If you can.. Use it:
使用 JavaScript 的另一种方法是将
Refresh
元标记添加到您的页面:content="2; url=..
中的两个告诉浏览器加载 url 2页面加载后的第二个。An alternative to using JavaScript is to add the
Refresh
meta tag to your page:The two in
content="2; url=..
tells the browser to load the url 2 second after the page has loaded.