PHP 脚本可以自行中止或检测中止吗?

发布于 2024-08-04 15:26:43 字数 258 浏览 3 评论 0原文

PHP 脚本可以 在 HTTP 页面请求后继续执行,那么当我完成它后如何最终停止它的执行呢?

另外,是否有一个事件可以检测操作系统何时将强制中止脚本?或者如何保持内部计时器来预测max_execution_time

PHP scripts can continue executing after the HTTP page request, so how do I finally stop it executing when I'm done with it?

Also, is there an event to detect when the OS is going to forcibly abort the script? or how do I keep an internal timer to predict max_execution_time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

情绪少女 2024-08-11 15:26:43

exit()/die() 将停止 PHP 脚本。

要知道何时停止脚本,您只需使用 microtime作为计时器并将最大执行时间保存为常量(或从 php.ini 文件中获取)。

您还可以查看连接处理信息。我们有诸如 connection_aborted()connection_status()

但是您要解决的问题是什么?

exit()/die() will stop a php script.

To know when to stop the script, you'll just have to use microtime as a timer and save as a constant (or fetch from the php.ini file) the maximum execution time.

You can also look at the Connection handling information. Where we have things like connection_aborted() and connection_status()

But what is the problem you're trying to solve?

如果没有 2024-08-11 15:26:43

您可以使用 register_shutdown_function 来注册一个在脚本执行结束时调用的函数。您可以使用它来检测脚本何时终止并执行一组特定的操作。

You can use register_shutdown_function to register a function to be called at the end of script execution. You can use this to detect when the script has been terminated and perform a certain set of actions.

屌丝范 2024-08-11 15:26:43

要在请求关闭时进行回调,请使用 register_shutdown_function( myfunction )

与大多数 POSIX 环境非常相似,PHP 也支持信号处理程序。您可以为 SIGTERM 事件注册自己的处理程序。

function my_abort_handler( $signo ) {
   echo "Aborted";
}

pcntl_signal( SIGTERM, "my_abort_handler" );

To have a callback at the moment your request is shutting down, use register_shutdown_function( myfunction ).

Much like most POSIX environments, PHP also supports signal handlers. You can register your own handler for the SIGTERM event.

function my_abort_handler( $signo ) {
   echo "Aborted";
}

pcntl_signal( SIGTERM, "my_abort_handler" );
各自安好 2024-08-11 15:26:43

您可能想看看 pcntl-alarm 它允许脚本向自身发送信号。还包含一些有关如何捕获操作系统发送的终止信号的示例。确实是 die() 。

You may want to take a look at pcntl-alarm which allows a script to send a signal to itself. Also contains some sample on how to catch the kill signals which can be send by the OS. And die() indeed.

躲猫猫 2024-08-11 15:26:43

好吧,您可以启动一个 $start=microtime(true) 它将返回一个时间戳。然后,您可以继续检查 microtime(true) 并从开始时间中减去该值,以获得自执行以来的秒数。

但是不,您无法“捕获”脚本由于请求太长而终止。您可以尝试在关闭处理程序中做一些最后一刻的事情,但我不确定 PHP 是否会遵守这一点。

看起来曾经有一个函数可以完全满足您的要求, connection_timeout( ),但已被弃用并删除。但不知道是否有任何替代品。

Well, you could start a $start=microtime(true) which will return a timestamp. Then you can just keep checking microtime(true) and subtract that from your start time to get the number of seconds since executing.

But no, you can't "catch" the script as its terminating for the reason of the request being too long. You could try to do some last minute stuff in the shutdown handler, but I'm not sure if PHP will honor that.

It looks like there used to be a function that does exactly what you want, connection_timeout(), but it was deprecated and removed. Don't know if there is any kind of replacement for this, however.

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