如何执行大型PHP脚本?

发布于 2024-09-01 12:05:08 字数 382 浏览 2 评论 0原文

基本上我可能想要执行一个可能需要长达 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

病毒体 2024-09-08 12:05:08

从命令行或 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.

甜宝宝 2024-09-08 12:05:08

从命令行运行的 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.

春庭雪 2024-09-08 12:05:08

如果您的主机允许,那么 cron 作业是最好的解决方案。 cron 作业基本上是一个普通的 php 脚本,由 Web 服务器在特定的时间间隔自动运行。根据您的需求,我将创建一个每 5 分钟运行一次的脚本,并以 100 个为一组处理您的数字(显然您需要调整时间间隔和批量大小以适应)。这将降低您的服务器负载,并防止您因占用资源而与托管提供商发生麻烦。

为了跟踪您的脚本应该处理哪个批次,我将设置一个 track_batch 表。这些列应该为您提供如何解决问题的良好指示:

id、date_run、start_record、end_record、final_run

本质上:

  • 检查最后一次的日期
    批量运行。如果不是当前日期
    (或您选择的任何其他标识符
    使用)当前批次,然后
    继续。
  • 如果最后一批运行当前日期,则检查
    Final_run 列查看是否
    你已经完成处理
    所有的数字。
  • 如果您仍有数字需要处理,请使用开始和结束
    与 MySQL 结合的记录
    LIMIT 构建您的数据库查询
    脚本将用于获取下一个
    批。
  • 处理你的数字。
  • 将此批次的所有信息存储在 track_batch 表中。
  • 如果查询返回的数字量小于最大值
    批量大小,您已到达终点
    并可以将 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:

  • 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.

謌踐踏愛綪 2024-09-08 12:05:08

使用 set_time_limit(0) 并不是最好的选择,因为这意味着即使您有错误并且脚本进入无限循环,它也会无限期地运行。

相反,如果您估计每条短信将花费 5 秒,请使用此方法:

while( $there_are_more_sms_to_be_sent ){
  set_time_limit(30); // enough spare time, just in case.

  // Do your sending, blah blah
}

这样,时间限制将依次更新为 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:

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.

Oo萌小芽oO 2024-09-08 12:05:08

在你可以运行 CRON 作业的情况下,

我通常有一个队列、一个管理器和工作人员。除非您可以一次调用短信 API,否则此模型可以帮助您,并且您不必担心超时,因为每个工作人员都会自行管理。

我有类似的东西:

<?php
// PSEUDO CODE
// grab pending from queue

// <for> {
// update to running
exec("/usr/bin/php /path/to/send.php {$id} > /dev/null &");
// }

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:

<?php
// PSEUDO CODE
// grab pending from queue

// <for> {
// update to running
exec("/usr/bin/php /path/to/send.php {$id} > /dev/null &");
// }

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

眼趣 2024-09-08 12:05:08

可以还是不能使用set_time_limit()

如果可以的话..使用它:

<?php
// Runs forever and ever...
set_time_limit(-1);
?>

You can or you can't use set_time_limit()?

If you can.. Use it:

<?php
// Runs forever and ever...
set_time_limit(-1);
?>
夏九 2024-09-08 12:05:08

使用 JavaScript 的另一种方法是将 Refresh 元标记添加到您的页面:

<meta http-equiv="Refresh" content="2; url=http://yoururl/script.php&step=x" ?>

content="2; url=.. 中的两个告诉浏览器加载 url 2页面加载后的第二个。

An alternative to using JavaScript is to add the Refresh meta tag to your page:

<meta http-equiv="Refresh" content="2; url=http://yoururl/script.php&step=x" ?>

The two in content="2; url=.. tells the browser to load the url 2 second after the page has loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文