gpt4 book ai didi

PHP 使用系统调用为脚本设置超时,set_time_limit 不起作用

转载 作者:IT王子 更新时间:2023-10-29 00:49:54 26 4
gpt4 key购买 nike

我有一个命令行 PHP 脚本,它使用带有 foreach 的数组的每个成员来运行 wget 请求。这个 wget 请求有时会花费很长时间,所以我希望能够设置一个超时来终止脚本,例如,如果它超过 15 秒。我禁用了 PHP 安全模式并在脚本的早期尝试了 set_time_limit(15),但它会无限期地继续。 更新:感谢 Dor 指出这是因为 set_time_limit() 不遵守 system() 调用。

所以我试图找到其他方法在执行 15 秒后终止脚本。但是,我不确定是否可以检查脚本在同时处于 wget 请求中间时的运行时间(do while 循环不起作用)。也许用计时器 fork 一个进程并将其设置为在设定的时间后杀死父进程?

感谢任何提示!

更新:下面是我的相关代码。 $url 是从命令行传递的,是一个包含多个 URL 的数组(抱歉最初没有发布):

foreach( $url as $key => $value){
$wget = "wget -r -H -nd -l 999 $value";
system($wget);
}

最佳答案

尝试使用 wget 命令行参数 --timeout 除了 set_time_limit()

请记住 set_time_limit(15) 从零重新启动超时计数器 所以不要在循环内调用它(为了您的目的)

来自 man wget:

--timeout=seconds

Set the network timeout to seconds seconds. This is equivalent to specifying --dns-timeout, --connect-timeout, and --read-timeout, all at the same time.

When interacting with the network, Wget can check for timeout and abort the operation if it takes too long. This prevents anomalies like hanging reads and infinite connects. The only timeout enabled by default is a 900-second read timeout. Setting a timeout to 0 disables it altogether. Unless you know what you are doing, it is best not to change the default time-out settings.

All timeout-related options accept decimal values, as well as subsecond values. For example, 0.1 seconds is a legal (though unwise) choice of timeout. Subsecond timeouts are useful for checking server response times or for testing network latency.

编辑: 好的。我明白你现在在做什么。您可能应该做的是使用 proc_open代替 system,并使用 time() 函数来检查时间,调用 proc_terminate如果 wget tskes 太长。

关于PHP 使用系统调用为脚本设置超时,set_time_limit 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2603912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com