gpt4 book ai didi

php exec() - max_execution_time 和 fatal error

转载 作者:行者123 更新时间:2023-12-04 04:41:52 25 4
gpt4 key购买 nike

在调整一堆图像(大约 9000 个)的大小时,我使用了这个函数:

function im_call($_data, &$_im_output, $_debug = IM_DEBUG) {

ini_set('max_execution_time', '3600');
exec($_data, $_im_output, $_im_error); // this is line 93
return $_im_error;

} // end function

过了一会儿(大约 30 分钟)PHP 死了:

fatal error :第 93 行的/some/path/im_lib.php 中超过了 30 秒的最大执行时间

(这是 exec()... 的行)

这怎么可能?

最佳答案

系统调用不计入 PHP 运行时间,因此几乎没有影响 max_execution_timeset_time_limit .如果您从网络服务器调用脚本,您必须知道网络服务器(不是 PHP)可能会断开 HTTP 连接。

例如:

Apache 默认配置:

# Timeout: The number of seconds before receives and sends time out.
#
Timeout 180

# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15

每 15 秒发送一次保持事件,在 HTTP 请求开始和连接关闭之间重复 100 次 = 1515 秒。这大约是25分钟,差不多半个小时。
即使这是关于 Webserver <> Client 协商而不是 Webserver <> PHP,webserver(和客户端!)仍然可能会在一段时间后简单地断开连接。

耗时超过几分钟的脚本应始终从控制台运行。 HTTP 服务器不会让单个请求保持数小时的事件状态。

从控制台(linux)使用 PHP 脚本:
#!/usr/bin/php
<?php
/* code */

结合 set_time_limit ,PHP 垃圾收集器和更多内存的设置,这些脚本可以运行很长时间。

附录:

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.



http://php.net/manual/en/function.set-time-limit.php

关于php exec() - max_execution_time 和 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18766989/

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