简体   繁体   English

如何在PHP中规避max_execution_time?

[英]How to circumvent the max_execution_time in PHP?

I try to mailing messages to many subscribers. 我尝试将邮件邮寄给许多订户。 I need to execute my script for a long time , much more longer that is allowed with max_execution_time . 我需要执行脚本很长时间 ,而max_execution_time允许的执行时间更长。 I can use Cron Tab , which will be execute my script every time by schedule, but can I do it without cron tab? 我可以使用Cron Tab ,它将按计划每次执行我的脚本,但是没有cron tab可以执行吗? I try something like this: 我尝试这样的事情:

    $maxTime = ini_get('max_execution_time');
    $startTime = time();
    foreach ($emails as $email) {
        if (time() < $startTime + $maxTime - 2) {
            // do something
        } else {
            // reload this page
        }
    }

And it's work well, but if I close this page in browser tab, it die and don't reload. 它运行良好,但是如果我在浏览器选项卡中关闭此页面,它将死并且不会重新加载。 I remind, that I'm looking for the implementation of this without Cron Tab . 我提醒一下,我正在寻找没有Cron Tab的实现 I want to start execution manually once and that to work it in the background later. 我想手动开始执行一次,然后在后台运行它。

NOTE: Also I want to note that I don't consider the load on the server and send mail possible interval at this stage! 注意:另外,我还要注意,在此阶段,我不考虑服务器上的负载和发送邮件的可能间隔!

You have a few options: 您有几种选择:

Command Line Your best option is to run this on the command line manually. 命令行最好的选择是在命令行上手动运行它。 Command line scripts are much better for running long running processes. 命令行脚本对于运行长时间运行的进程要好得多。

Shell Exec If you must run this via browser, you can trigger the command line script by using exec eg shell_exec('php -f /var/www/domain.com/myLongRunningProcess.php > /dev/null 2>/dev/null &') Shell Exec如果必须通过浏览器运行它,则可以使用exec来触发命令行脚本,例如shell_exec('php -f /var/www/domain.com/myLongRunningProcess.php > /dev/null 2>/dev/null &')

Ignore User Abort You can also run things after the browser has detached from a browser session. 忽略用户中止您也可以在将浏览器从浏览器会话中分离后运行程序。 This is the most complex and hardest to debug but it will work. 这是最复杂,最难调试的,但是可以使用。 You must calculate the exact size of the page and then send the output and use the function ignore_user_abort so that your script will continue to run when the browser disengages. 您必须计算页面的确切大小,然后发送输出并使用功能ignore_user_abort以便在浏览器脱离时脚本可以继续运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM