在Windows机器上的PHP中长时间运行的后台进程

时间:2014-10-08 10:56:12

标签: php background-process asynchronous

我在PHP中有一个脚本,它使用Curl逐个列出一长串URL,并在最后将响应写入文件,我希望从浏览器上传列表,然后执行其余的处理。背景(不让用户等待响应)。我已经尝试过以下解决方案 -

    $command = "C:\wamp\bin\php\php5.5.12\php.exe ../background_process/subscribe_bg.php ".$file_temp_path;
    shell_exec(sprintf('%s > /dev/null 2>/dev/null &', $command));

这会成功运行脚本,但会使浏览器等待。 (这可能会在Linux机器上运行。)

    $command = "C:\wamp\bin\php\php5.5.12\php.exe ../background_process/subscribe_bg.php ".$file_temp_path;

    execInBackground($command);

    function execInBackground($cmd) { 
        if (substr(php_uname(), 0, 7) == "Windows"){ 
            pclose(popen("start /B ". $cmd, "r"));  
        } 
        else { 
            exec($cmd . " > /dev/null &");   
        } 
    }

我为Windows机器找到了这个解决方案,但对我来说不起作用。脚本根本不执行。

请建议在Windows机器上使用PHP在后台运行一个漫长的过程(不是很长~30-40分钟)的最佳做法。

1 个答案:

答案 0 :(得分:2)

这适用于Windows机器,脚本的所有输出都写入$ response_file_path -

$command = $PHP_DIR." ../background_process/subscribe_bg.php -p=".$file_path_arg." >../sublogs/responses/".$file_response_path." 2>../sublogs/error_logs/err.txt";

execInBackground($command)

function execInBackground($cmd) {
    if (substr(php_uname(), 0, 7) == "Windows"){
        pclose(popen("start /B ". $cmd, "r"));
        return 1;
    }
    else {
        return 0;
        //Or the code for linux machine.
    }
}