dongyi2083 2010-12-06 15:59
浏览 35
已采纳

PHP脚本应该在失败时继续工作

I know people complain usually about scripts not working, but here is a case where it keeps working even if I want it to stop.

I have a CSV parser that analyzes lines and inserts entries in a DB table. I am using PDO and Zend Framwork for the project. The code works fine.. too fine in fact.

public function save()
{
    $memory_limit = ini_get('memory_limit');
    ini_set('memory_limit', '512M');

    $sql = "
        INSERT INTO my_table (
            date_start,
            timeframe,
            type,
            country_to,
            country_from,
            code,
            weight,
            value               
        ) VALUES (?,?,?,?,?,?,?,?)
        ON DUPLICATE KEY UPDATE 
            weight = VALUES(weight),
            value = VALUES(value)
    ";
    if ($this->test_mode) {
        echo $sql;
        return;
    }
    $stmt = new Zend_Db_Statement_Pdo($this->_db, $sql);
    foreach($this->parsed_data as $entry){
        $stmt->execute(array_values($entry));
        $affected_rows = $stmt->rowCount();
        if ($affected_rows){
            $this->_success = true;
        }
    }
    unset($this->parsed_data, $stmt, $sql);
    ini_set('memory_limit', $memory_limit);
}

The script takes various seconds to complete as I am parsing a big file. The problem appears when I am trying to stop the script, with ESC or even by closing the page. The script does not stop until it finishes to insert all entries. Not even an Apache reload is not fixing this, probably a restart will do it.

I am thinking that this is not normal behaviour and maybe I am doing something wrong so I am asking for suggestions.

Thanks.

UPDATE ignore_user_abort is off (default behaviour) so user abort should be considered..

  • 写回答

2条回答 默认 最新

  • duanbai1027 2010-12-06 16:02
    关注

    I'm pretty sure that's standard PHP behaviour - just because the browser goes away doesn't mean it won't stop processing the script. (Although restarting Apache, etc. will achieve this goal.)

    To change this behaviour, you can use ignore_user_abort.

    That said, "PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client", which I suspect may be the issue you're experiencing.

    See the above link and the PHP runtime configuration information for more info.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?