PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-使用Symfony Process组件进行Ajax轮询大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在启动一个长期运行的任务,该任务返回有关Symfony Process组件任务进度的增量输出.

一个示例显示了如何获取实时输出,另一个示例显示了如何运行异步任务.

我要实现的目标是将geTincrementalOutput的结果传递回ajax轮询功能,以便我可以实时更新前端.

在这两种情况下,似乎进程-> start()都被阻塞,因为我的ajax调用需要一分钟才能返回,并且到那时该任务已完成.

我想我想避免将进度写入数据库文件,而直接从运行的PHP任务获取输出.

不确定是否有可能.

解决方法:

尽管我不完全了解您要创建的内容,但是我已经写了类似的内容,看着它可能会回答您的问题:

首先,我创建了一个执行长期任务的命令:

class GenerateCardBarcodesCommand extends Command 
{
    protected function configure()
    {
        $this
            ->setName('app:generate-card-barcodes')
            ->setDescription('Generate the customer cards with barcodes')
            ->addArgument('id', InputArgument::required, 'id of the Loy/Card entity')
        ;
    }

    protected function execute(InpuTinterface $input, OutpuTinterface $output)
    {
        $id = $input->getArgument('id');
        // generate stuff and put them in the database
    }
}

在控制器中,该过程已启动,并且有一个ajax操作

class CardController extends Controller
{
    public function newAction(request $request)
    {
                // run command in BACkground to start generating barcodes
                // NOTE: unset DYLD_LIBRARY_PATH is a fix for MacOSX develop using MAMP.
                // @see https://stackoverflow.com/questions/19008960/phantomjs-on-mac-os-x-works-from-the-command-line-not-via-exec
                $process = new Process(sprintf('unset DYLD_LIBRARY_PATH ; PHP ../../apps/spin/console spin:loy:generate-card-barcodes %d', $entity->getId()));
                $process->start();

                sleep(1); // wait for process to start

                // check for errors and output them through flashbag
                if (!$process->isRunning())
                    if (!$process->issuccessful())
                        $this->get('session')->getFlashBag()->add('error', "Oops! The process fininished with an error:".$process->getErrorOutput());

                // otherwise assume the process is still running. It's progress will be displayed on the card index
                return $this->redirect($this->generateUrl('loy_card'));
     }

    public function ajaxCreateBarcodesAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $this->getEntity($id);
        $count = (int)$em->getRepository('ExtendasSpinBundle:Loy\CustomerCard')->getCount($entity);
        return new Response(floor($count / ($entity->getNoCards() / 100)));
    }
}

//在树枝模板中检索ajax,它只是一个介于0到100之间的数字,用于jquery ui进度栏中.
    {{‘正在处理’| trans}} …

            <script type="text/javascript">
            $(function() {
                function pollLatestResponse() {
                    $.get("{{ path('loy_card_ajax_generate_barcodes', {'id': entity[0].iD}) }}").done(function (perC) {
                        if (perc == 100)
                        {
                            clearInterval(pollTimer);
                            $('#download-{{entity[0].iD}}').show();
                            $('#progress-{{entity[0].iD}}').hide();
                        }
                        else
                        {
                            $('#progress-{{entity[0].iD}}').progressbar("value", parseInt(perC));
                        }
                    });
                }

                var pollTimer;
                $(document).ready(function () {
                    $('#progress-{{entity[0].iD}}').progressbar({"value": falsE});
                    pollTimer = seTinterval(pollLatestResponse, 2000);
                });
            });
            </script>

大佬总结

以上是大佬教程为你收集整理的php-使用Symfony Process组件进行Ajax轮询全部内容,希望文章能够帮你解决php-使用Symfony Process组件进行Ajax轮询所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: