2

[编辑的 OP 在这里是简短的版本]

循环遍历文件并读取内容,然后写入会导致函数失败。这似乎是一个内存问题。这是我试过的三个版本。

首先尝试了这个:

$file = new SplFileObject($this->getDirectoryPath() . $this->getFileName(), "a+");
$file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);

if ($this->exists()) {
    foreach ($file as $line) {
        $tempArray = unserialize($line);
        if ($tempArray['Key'] == $arrayOfData['Key']) {
            foreach ($totalsToBeAdded as $key) {
                $arrayOfData[$key] += $tempArray[$key];
            }
        }
    }
}

$tempString = serialize($arrayOfData);

$file->fwrite("$tempString\r\n");

$this->numLines++;

然后我尝试了这个:

$file = new SplFileObject($this->getDirectoryPath() . $this->getFileName(), "a+");
$file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);

if ($this->exists()) {
    while (!$file->eof()) {
        $tempArray = unserialize($file->current());
        if ($tempArray['PartNumber'] == $arrayOfData['PartNumber']) {
            foreach ($totalsToBeAdded as $key) {
                $arrayOfData[$key] += $tempArray[$key];
            }
        }

        $file->next();
    }
}

$tempString = serialize($arrayOfData);

$file->fwrite("$tempString\r\n");

$this->numLines++;

最后我放弃了 SplFileObject 并使用了普通的 fopen 等:

$handle = fopen($this->getDirectoryPath() . $this->getFileName(), "a+");

if ($this->exists()) {
    while (false !== ($line = fgets($handle))) {
        $tempArray = unserialize(trim($line));
        if ($tempArray['Key'] == $arrayOfData['Key']) {
            foreach ($totalsToBeAdded as $key) {
                $arrayOfData[$key] += $tempArray[$key];
            }
        }
    }
}

$tempString = serialize($arrayOfData);
fwrite($handle, "$tempString\r\n");
fclose($handle);
$this->numLines++;

编辑更多信息:

我很好奇 PHP 的底层代码在逐行遍历文件时是否使用数组作为迭代器,这可能会杀死它。

而且文件确实开始构建,我可以看到它写入到大约 500-600k 然后它就死了。

最终文件大小约为 10mb。

最后一次更新:

这有效(注意缺少打开和读取文件):

public function writeUnique($arrayOfData, $totalsToBeAdded) {  
        $tempArray = array();

        $handle = fopen($this->fullPath, "a+");

        $tempString = serialize($arrayOfData);
        fwrite($handle, "$tempString\r\n");
        fclose($handle);
        $this->numLines++;
}

虽然这会中断(注意所有正在执行的操作都是循环整个文件然后写入文件):

public function writeUnique($arrayOfData, $totalsToBeAdded) {  
        $tempArray = array();

        $handle = fopen($this->fullPath, "a+");

        if ($this->exists()) {
            while (false !== ($line = fgets($handle))) {

            }
        }

        $tempString = serialize($arrayOfData);
        fwrite($handle, "$tempString\r\n");
        fclose($handle);
        $this->numLines++;
}

更新三号:

我现在已经对此进行了测试:

public function writeUnique($arrayOfData, $totalsToBeAdded) {

    $handle = fopen($this->fullPath, "a+");

    if ($this->exists()) {
        while (false !== ($line = fgets($handle))) {

        }
    }

    $tempString = serialize($arrayOfData);
//        fwrite($handle, "$tempString\r\n"); Commented out the writing.
    fclose($handle);
    $this->numLines++;
}

这行得通。没有失败,内存错误或其他方面。

因此,这似乎要么是重读大文件的相同行的迭代问题,要么是函数的写入部分在某种程度上踩到了读取函数的脚趾。老实说,这没有意义. 我知道每个人都认为这与我的阵列有关。但是我已经充分利用了我所有的逻辑,我只是​​想读/写一个大文件。

4

2 回答 2

0

尝试:

if ($this->exists()) {
    while (false !== ($line = fgets($handle))) {
        $tempArray = unserialize(trim($line));
        unset($line);
        if ($tempArray['Key'] == $arrayOfData['Key']) {
            foreach ($totalsToBeAdded as $key) {
                $arrayOfData[$key] += $tempArray[$key];
            }
        }
        unset($tempArray);
    }
}

我在这里可以看到的唯一持久数组是$totalsToBeAddedand $arrayOfData,从您的+=运算符看来它是一维的,因此您无能为力,只能进行微优化。

于 2012-11-02T15:44:06.770 回答
0

所以我终于崩溃了,计算了一下我需要 php 在这个文件上完成多少个循环,这个数字是 8,788,338,000,000 次。

这反过来又导致 PHP 超时。为了防止它超时,需要添加这行代码。

set_time_limit(0); // ignore php timeout

现在可以逐行读取和解析临时文件。但是,在大文件(10 mb +)上,完成该功能的时间到目前为止已经超过一个小时(它仍在运行,因为我可以看到临时文件越来越大)。

我得出的结论是,如果速度至关重要,那么将 LARGE 数据集存储到临时 SQL 表中可能会更好。这以前对我来说不是一个选择,但现在我正在用允许它的权力来强迫这个问题。最坏的情况下,这将至少允许它运行。

请注意:这将允许无限循环永远运行并可能杀死服务器。在尝试之前,请确保您知道如何通过 UNIX 终止进程。

于 2012-11-05T16:18:14.810 回答