php exec()-max_execution_time和致命错误 [英] php exec() - max_execution_time and Fatal error

查看:170
本文介绍了php exec()-max_execution_time和致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调整一堆图像的大小(大约9000张)时,我使用了以下功能:

While resizing a bunch of images (about 9000), I use this function:

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死于:

And after a while (about 30 MINUTES) PHP dies with:

致命错误:第93行的/some/path/im_lib.php中超过30秒的最大执行时间

Fatal error: Maximum execution time of 30 seconds exceeded in /some/path/im_lib.php on line 93

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

(it is the line with exec()... )

这怎么可能?

推荐答案

系统调用不计入PHP运行时,因此对max_execution_timeset_time_limit几乎没有影响.如果要从Web服务器调用脚本,则必须知道该Web服务器(而非PHP)可能会断开HTTP连接.

System calls do not count to the PHP run time and so barely effect max_execution_time and set_time_limit. If you are calling your script from a webserver, you have to know that the webserver (not PHP) may drop the HTTP connection.

例如:

Apache默认配置:

Apache default configuration:

# 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<>客户端协商而不是Webserver<> PHP,Web服务器(和客户端!)仍然可以在一段时间后简单地断开连接.

Every 15 seconds, send a keep alive, repeat 100 times = 1515 seconds between HTTP Request start and closing of the connection. This is about 25 Minutes, almost half an hour. Even if this is about Webserver <> Client negotiation and not Webserver <> PHP, the webserver (and client!) still may simply drop the connections after a while.

耗时超过几分钟的脚本应始终从控制台运行. HTTP服务器不建议将单个请求保留几个小时.

Scripts taking longer than a few minutes should always run from console. HTTP Servers are not ment to keep a single request alive for hours.

从控制台(Linux)使用PHP脚本:

Use PHP scripts from console (linux):

#!/usr/bin/php
<?php
/* code */

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

In combination with set_time_limit, the PHP garbage collector and the settings for more memory, those scripts can run for ages.

附录:

set_time_limit()函数和配置指令 max_execution_time仅影响脚本的执行时间 本身.花在执行之外的活动上的任何时间 脚本,例如使用system()的系统调用,流操作, 确定最大数量时不包括数据库查询等 脚本已运行的时间.在Windows上不是这样 测量的时间是真实的.

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和致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆