4

Symfony2 アプリケーションのコントローラー内で長時間持続するコマンドを実行し、端末の出力をリアルタイムでユーザーに返す必要があります。

私はこれを読みました:

http://symfony.com/doc/current/components/process.html#getting-real-time-process-output

Twig テンプレートで端末出力をリアルタイムで印刷する方法がわかりません。

編集: Matteo のコードとユーザーのコメントのおかげで、最終的な実装は次のとおりです。

/**
 * @Route("/genera-xxx-r", name="commission_generate_r_xxx")
 * @Method({"GET"})
 */
public function generateRXXXsAction()
{
    //remove time constraints if your script last very long
    set_time_limit(0);        

    $rFolderPath = $this->container->getParameter('xxx_settings')['r_setting_folder_path'];
    $script = 'R --slave -f ' . $rFolderPath . 'main.R';

    $response = new StreamedResponse();
    $process = new Process($script);
    $response->setCallback(function() use ($process) {
        $process->run(function ($type, $buffer) {
            //if you don't want to render a template, please refer to the @Matteo's reply
            echo $this->renderView('AppBundle:Commission:_process.html.twig',
                array(
                    'type' => $type,
                    'buffer' => $buffer
                ));
            //according to @Ilmari Karonen a flush call could fix some buffering issues
            flush();
        });
    });
    $response->setStatusCode(200);
    return $response;
}
4

1 に答える 1