无法实时从redis读取

Cannot realtime read from redis

我有一个服务器长期 运行 应用程序,其存储过程如下:

routes.php

Route::post('cloud/slice/{modelfileid}', 'CloudController@slice');
Route::get('cloud/slice/sliceProgress', 'CloudController@sliceProgress');

部分 slice() 函数:

while($s = fgets($pipes[2]))
{
    if(strpos($s, "Progress:") !== FALSE)
    {
        Log::info(100 * substr($s, -10, 4) . '%');
        $this->redis->SET('sliceProgress' . $uid, 100 * substr($s, -10, 4) . '%');

    }
}

我的客户每 0.5 秒请求一次 redis 的进度:

public function sliceProgress()
{
    if (!isset($_SESSION["uid"]))
        return Redirect::to('cloud');
    $uid = $_SESSION["uid"];
    return Response::json(array('status' => 'success', 'data' => array('progress' => $this->redis->GET('sliceProgress' . $uid))));
}

$interval(function()
{
    $scope.refreshProgress();
}, 500);

但是客户端获取进度请求会一直保持到100%结束,没有低于100%的进度。也就是说,当服务器切片应用程序完成时,我得到了进度响应。我想 laravel 在处理切片时不处理第二个请求。


Edited: I use below code to get the redis in the slice() after the redis->SET works well, this output the sliceProgress realtime.

Log::info($this->redis->GET('sliceProgress' . $uid));

尝试 运行 在异步队列或 crons 等后台进程中使用此代码

while($s = fgets($pipes[2]))
{
if(strpos($s, "Progress:") !== FALSE)
{
    Log::info(100 * substr($s, -10, 4) . '%');
    $this->redis->SET('sliceProgress' . $uid, 100 * substr($s, -10, 4) . '%');

}
}

我认为问题可能出在会话锁上。假设您在多次 GET 调用之后触发了 POST 请求。现在,如果存在会话锁,get 请求的 none 将由 PHP 处理,直到第一个完成。

会话锁如何工作?
好吧 php 只需锁定要读取的会话文件,因此所有其他 apache 进程都必须等待。 Laravel 也使用基于文件的会话管理,所以。

现在删除会话锁定不是一个好主意,尽管您可以这样做。尝试从不同的主机或端口公开此获取服务。

我不是 100% 确定 laravel 如何处理会话锁定,所以请交叉检查是否是这个问题。

删除会话锁:
第一种方式:完成会话处理后使用方法 session_write_close.
第二:在使用 session_start

开始会话时将 read_and_close 设为 true

不确定 laravel 是否允许这样做。还有其他方法可以进行实时进度同步。