PHP 脚本即使在 exit() 之后继续执行
PHP script continues execution even after exit()
所以我有一个相对简单的 PHP 脚本
从远程休息服务读取 ~ 70 XML 个文件;
使用 SimpleXMLElement class 解析读取 XML 文件(许多新的 SimpleXMLElement() 调用);
输出到文件 json 编码数组,该数组包含关联数组,其中每个数组都有来自那些 XML 文件的一些感兴趣的属性。
脚本的问题在于它完成的时间比将结果输出到文件的时间晚得多。我用谷歌搜索但找不到任何可能导致此类问题的东西。下面是描述我的脚本做什么的简化代码。
function parseFiles()
{
$fileData = array();
$parsedData = array();
// read files using curl, output is an array of SimpleXMLElements
readXMLFiles($fileData);
// for each XML object create an assoc array which contains attributes
// of interest, add it to $parsedData
parseData($fileData, $parsedData);
file_put_contents("test.txt", json_encode($parsedData));
/*
This is where the problem occures, the scipt outputs result to the file
MUCH faster than it ends execution for example file is created with data
in ~ 15 seconds but the script ends in 60 seconds
(so the exit() command took 45 seconds????)
*/
exit();
}
这会不会是在到达 exit() 时由某种垃圾收集引起的?那些 XML 对象相对较大...我还尝试 运行 wamp 堆栈上的脚本和 PHP,apache 运行ning 在 centos 上,问题似乎在centos机器上发生。
问题是由解决问题的 SimpleXML class. No idea what was happening, but I came across issue where someone found that it was causing memory leaks, so I rewrote my code to work with DOMDocument class 引起的。
之前脚本会挂起约 2 分钟,重写后它会在 13 秒内完成 ;)
所以我有一个相对简单的 PHP 脚本
从远程休息服务读取 ~ 70 XML 个文件;
使用 SimpleXMLElement class 解析读取 XML 文件(许多新的 SimpleXMLElement() 调用);
输出到文件 json 编码数组,该数组包含关联数组,其中每个数组都有来自那些 XML 文件的一些感兴趣的属性。
脚本的问题在于它完成的时间比将结果输出到文件的时间晚得多。我用谷歌搜索但找不到任何可能导致此类问题的东西。下面是描述我的脚本做什么的简化代码。
function parseFiles()
{
$fileData = array();
$parsedData = array();
// read files using curl, output is an array of SimpleXMLElements
readXMLFiles($fileData);
// for each XML object create an assoc array which contains attributes
// of interest, add it to $parsedData
parseData($fileData, $parsedData);
file_put_contents("test.txt", json_encode($parsedData));
/*
This is where the problem occures, the scipt outputs result to the file
MUCH faster than it ends execution for example file is created with data
in ~ 15 seconds but the script ends in 60 seconds
(so the exit() command took 45 seconds????)
*/
exit();
}
这会不会是在到达 exit() 时由某种垃圾收集引起的?那些 XML 对象相对较大...我还尝试 运行 wamp 堆栈上的脚本和 PHP,apache 运行ning 在 centos 上,问题似乎在centos机器上发生。
问题是由解决问题的 SimpleXML class. No idea what was happening, but I came across issue where someone found that it was causing memory leaks, so I rewrote my code to work with DOMDocument class 引起的。
之前脚本会挂起约 2 分钟,重写后它会在 13 秒内完成 ;)