dousi1906 2012-03-19 18:11
浏览 72
已采纳

PHP批量处理解决方案

I'm looking for a PHP component for asynchronous data processing.

Basically what I need is to display a page with a progress bar that's refreshed with javascript which displays the progress on some data processing.

On the backend you'll define your data process limit. This is the start, end and function to call for processing individual items.

There are plenty of solutions for this on CMS and frameworks. I'm looking for something in raw PHP that I can include in my application.

  • 写回答

1条回答 默认 最新

  • douwen9540 2012-03-19 19:42
    关注

    I did something similar not too long ago. I wrote a function that logs the progress to a text file as a JSON object. Then I wrote a PHP function that returns that JSON object to the browser at certain intervals as requested by jQuery.

    My PHP code looks similar to this:

    function logProgress($task, $status, $progress) {
    
    
    $basedir = "/var/www/" . SITE_ROOT . "/";
    $log_file = $basedir . "logs/progress.log";
    
    $logFileContent = file_get_contents($mrp_log_file);
    
    if($logFileContent){
        $logFileArray = json_decode($logFileContent, TRUE);
    } else {
        $logFileArray = array();
    }
    
    $logFileArray[$task]=array('task'=>$task,'status'=>$status,'progress'=>$progress);
    
    $logFile = fopen($log_file, 'w+') or error_log("Failed to open progress file $mrp_log_file for writing");
    
    fwrite($logFile, json_encode($logFileArray));
    
    fclose($logFile);
    
    }
    

    Retrieving the data is as simple as this:

    function readProgressLog() {
    //Returns a JSON object stored in the progress log.
    $basedir = "/var/www/" . SITE_ROOT . "/";
    $log_file = $basedir . "logs/progress.log";
    
    $logFileContents = file_get_contents($log_file);
    
    return $logFileContents;
    

    }

    From jQuery, you would make two AJAX calls, one to initiate your process, and one to poll the text file. My javascript for the polling call looks like this:

    function updateProgress() {
    var data = {
        action:'getProgressUpdate'};
    var settings = {success: function(json){
        var done = false;
    
        if(json!=null) {
            //Put your code to update the progress bar here.
                        //I look for a JSON property called Done to flag the process as completed.                      
            if(json.Done==null) {
                var t2 = setTimeout("updateProgress()", 1000);
            } else {
                clearTimeout(t2);
                done = true;
                clearProgressLog();
            }
        } else {
             var t2 = setTimeout("updateProgress()", 1000);
        }
    },
    data:data,
    cache:false,
    type: 'POST',
    dataType:"json"};
    
    $.ajax('/ajax/polling.ajax.php', settings);
    

    }

    One thing I noticed is that you should make sure your polling AJAX call uses a different PHP file than your process AJAX call, otherwise your polling call won't finish until the process call is finished.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探