Php 为什么对Symfony控制器的jqueryajax请求是并行处理而不是异步处理的?

Php 为什么对Symfony控制器的jqueryajax请求是并行处理而不是异步处理的?,php,jquery,ajax,symfony,asynchronous,Php,Jquery,Ajax,Symfony,Asynchronous,使用jQuery$将简单数据发布到普通PHP脚本时。ajax({…})会并行处理多个请求。当使用Symfony 2.8控制器作为目标执行相同操作时,会同步处理请求这是为什么? 普通HTML和PHP设置 // Plain PHP file: /testscript.php <?php sleep($_POST['time']); echo $_POST['id']; // Plain HTML file: /testpage.html <html> <

使用
jQuery
$将简单数据发布到普通
PHP
脚本时。ajax({…})
会并行处理多个请求。当使用
Symfony 2.8
控制器作为目标执行相同操作时,会同步处理请求这是为什么?

普通HTML和PHP设置

// Plain PHP file: /testscript.php
<?php 
    sleep($_POST['time']);
    echo $_POST['id'];


// Plain HTML file: /testpage.html
<html>
<head>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
Click here:
<div id='testbtn' style="background-color: #abc">Click</div>

 <script>
    $(document).ready(function() {
        var start = new Date().getTime();
        var count = 1;

        $("#testbtn").click(function() {
            var time = new Date().getTime();
            console.log('Click at '+count+':' + (time - start));

            $.ajax({
                url : '/testscript.php',
                type : "post",
                data : {'time':3, 'id':count},
                async: true,
                context : this,
                success : function(data) {
                    var end = new Date().getTime();
                    console.log('Click Success: ' + data + "  -  " + (end - start));
                }
            });

            count++;
        });


        $.ajax({
            url : '/testscript.php',
            type : "post",
            data : {'time':10, 'id':0},
            async: true,
            context : this,
            success : function(data) {
                var end = new Date().getTime();
                console.log('Auto Success: ' + data + "  -  " + (end - start));
            }
        });

        console.log('Ajax fired');
    });
</script>

</body>
</html>    
// Controller Action to handle /sym_testscript.php
public function testScriptAction(Request $request) {
    sleep($request->get('time'));
    return new Response($request->get('id'), Response::HTTP_OK);
}


// Controller Action to handle /sym_testpage.php
public function testPageAction() {
    return $this->render('MyBundle::sym_testpage.html.twig');
}   


// Twig-Template for /sym_testpage.html
...exactly the same HTML code as above. Twig is only used to insert URL
...
$.ajax({
    url : '{{ path('sym_testscript') }}',
    ...
当加载睡眠值为10秒时,页面
/testpage.html
调用
/testscript.php
。单击按钮几次后,页面加载瀑布图如下所示:

1: ========================================   // initial call of testscript.php
2:     ============                           // testscript.php called by 1 click
3:      ============                          // testscript.php called by 2 click
4:         ============                       // testscript.php called by 3 click
1: ========================================   // initial call of testscript.php
2:     ====================================================                           
3:      ================================================================                          
4:         ============================================================================
每次单击按钮都会立即调用
testscript.php
,然后与初始调用和其他按钮调用并行执行。因此,每次单击调用都会运行3秒钟

当改用Symfony版本时,瀑布图如下所示:

1: ========================================   // initial call of testscript.php
2:     ============                           // testscript.php called by 1 click
3:      ============                          // testscript.php called by 2 click
4:         ============                       // testscript.php called by 3 click
1: ========================================   // initial call of testscript.php
2:     ====================================================                           
3:      ================================================================                          
4:         ============================================================================
同样,每次单击按钮都会立即调用
/sym_testscript.php
。但是现在电话一个接一个地被处理。因此,总运行时间不是10秒,而是19=10+3+3+3

当使用普通HTML文件中的
sym_testscript.php
作为目标时,结果是相同的。因此,问题似乎在
Symfony
控制器内

这是为什么?
为什么在使用
Symfony
解决方案时不并行处理ajax调用?

一旦在php中启动会话,php将锁定它,后续请求将不得不等待会话再次可用

因此,如果symfony脚本使用会话,则在会话打开时,使用该会话一次只能执行1个请求


禁用会话(如果这是一个选项…)或在不再需要时关闭会话将允许并行请求。

您是否使用PHP内置服务器运行symfony应用程序(如使用
PHP应用程序/控制台服务器:运行
)?如果是这样,此服务器一次只能处理一个请求。尝试使用Apache或Nginx,它应该可以正常工作否,这是在浏览器中调用页面时的结果。在我的本地计算机(XAMPP)和我的提供商的共享主机Web空间上运行
Symfony
时,结果是相同的。能否为我们提供两种设置的虚拟主机配置?Symfony脚本是否使用会话?PHP会话被请求锁定,因此使用同一会话的多个请求将按顺序运行。@jeroen非常感谢您关于会话的提示。这似乎确实是问题所在!将
$request->getSession()->save()
添加到控制器后,它的行为符合预期(并行而不是顺序)。如果这是一个答案,我会接受:-)这取决于您使用的是本机PHP会话还是自定义处理程序,@AndreasBergström这可能是真的,但据我所知,这只是指数据存储的位置,而不是会话本身的管理方式。如果您有任何特别提到会话阻塞的文档,我将非常有兴趣看到它们:-)是的,我离开服务器呈现的应用程序太久了,脑子里只有基于令牌的会话。因此,是的,我支持PHPs本机会话仍在使用。:)