【发布时间】:2016-06-09 18:41:03
【问题描述】:
用户提交表单后,我想渲染一个视图文件,然后我想启动一个后台任务来处理五个 MS Excel 文件(每个可能有多达 2000 行),但方式是这样用户不必等待进程完成即可查看页面。任务完成后我会通过邮件通知用户。
我正在使用 Symfony Framework 3。我在下面包含了我的代码。它没有做我想要实现的目标。提交表单后,页面仅在整个后台任务完成时加载。
我不确定,但在谷歌上搜索了很多之后,我认为The kernel.terminate Event 可能在这里有用。但我似乎无法理解如何使用它。
你能告诉我如何解决这个问题吗?
这是我的代码:
我已经创建了一个控制台命令:
class GreetCommand extends ContainerAwareCommand {
protected function configure()
{
$this->setName('hello:world');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// My code to execute
}
}
在我的控制器中我有
$process = new Process('ls -lsa');
$process->disableOutput();
$that = $this;
$process->start(function ($type, $buffer) use ($that) {
$command = new GreetCommand();
$command->setContainer($this->container);
$input = new ArrayInput(array());
$output = new NullOutput;
$resultCode = $command->run($input, $output);
});
return $this->render('success.html.php', array('msg' => 'Registraion Successful'));
更新
我已经使用 PHP 的连接处理功能解决了这个问题。
感谢post
【问题讨论】:
-
用一些关于连接处理功能实现的信息来更新你的帖子会很有用!
-
@mpilliador - 您可以简单地在 kernel.terminate 上创建一个事件侦听器或异步运行进程(请参阅下面的 Denis Alimov 回答)。