php - 无法实时从redis读取

标签 php laravel controller redis real-time

我有一个服务器长时间运行的应用程序,它存储这样的过程:

路由.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) . '%');

}
}

关于php - 无法实时从redis读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41254473/

相关文章:

javascript - 如何从创建许多选择标签的指令中将项目列表添加到 Controller 范围

laravel - laravel 中的 show 方法返回 index 方法

javascript - Ruby on rails - 如何让 Controller 与 ajax 同时执行 2 个 Action ?

php - 混淆组和带有 php 的子句

php - 有没有用PHP维护MySQL表结构的库?

php - 在 MySQL 中进行复杂的数组查询

php - 创建一个与团队见面的页面,想要按降序排列,每个级别到新行

php - Laravel:使用自定义请求 header 测试路由

mysql - Laravel - 时差

codeigniter 无法加载库