发送两个ajax请求,同时在symfony中 [英] send two ajax request at the same time in symfony

查看:197
本文介绍了发送两个ajax请求,同时在symfony中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我一起发送使用JQuery ..反应过来在一起的两个Ajax请求

When i send two ajax requests together using JQuery .. the response come together

$.ajax
({
    type: "POST",
    url: 'ajax.php'
});
$.ajax
({
    type: "POST",
    url: 'ajax2.php'
});

ajax.php,ajax2.php两个文件包含一个虚拟的循环需要大约5秒。

ajax.php , ajax2.php are two files contain a dummy for loop take about 5 sec.

POST本地主机/ ajax.php 200 OK 4.77s
POST本地主机/ ajax.php 200 OK 4.37s

POST localhost/ajax.php 200 OK 4.77s
POST localhost/ajax.php 200 OK 4.37s

下面的每个请求大约需要5秒被执行.....

Here every request take about 5 sec to be executed .....

当我做同样的例子在symfony中,我得到不同的结果。

When i do the same example at symfony i got different result

$.ajax
({
    type: "POST",
    url: 'module/action1'
});
$.ajax
({
    type: "POST",
    url: 'module/action2'
});

动作1,动作2是两个动作只包含一个虚拟的循环需要大约5秒。

action1 , action2 are two action just contain a dummy for loop take about 5 sec.

POST本地主机/网络/ frontend_dev.php /模块/动作1 200 OK 4.47s

POST localhost/web/frontend_dev.php/module/action1 200 OK 4.47s

POST本地主机/网络/ frontend_dev.php /模块/动作2 200 OK 9.87s

POST localhost/web/frontend_dev.php/module/action2 200 OK 9.87s

请注意执行后第一个完成了第二个请求,我不知道为什么会发生

Notice that the second request executed after the first one finished , i don't know why that happened

推荐答案

当一个请求时,它会尝试启动一个会话,PHP将检查同一个会话中使用的那一刻。如果是,新的请求必须等待,直到其他请求完成,或释放会话锁。

When a request comes in, and it tries to start a session, php will check if the same session is in use at that moment. If it is, the new request has to wait till the other request finishes, or releases the session lock.

您的情况是这样的:

  • 在第一个请求到达时,锁定会议
  • 第二reauest到达时,试图锁定会话,必须等待
  • [5秒睡眠]
  • 在第一个请求完成后,释放锁
  • 第二个请求启动,实现了会话锁定
  • [5秒睡眠]
  • 第二个请求完成

在默认情况下的symfony在每个请求开始启动会话。

By default symfony starts the session at the beginning of every request.

在纯PHP,你可以用 session_write_close() 。 Symfony的有它包装会议functinality的sfUser中类,你就需要调用它的 关机() 的方法。

In pure PHP you can release the session file's lock with session_write_close(). Symfony has the sfUser class which wraps session functinality, you'll need to call it's shutdown() method.

被告知,如果在该请求后修改任何会话数据,它不会保存。

Be advised that if you modify any session data later in that request, it will not get saved.

有关更详细的解释,阅读的 PHP会话写入锁定,以及如何处理它的symfony

For a more detailed explanation, read PHP Session write locking and how to deal with it in symfony.

这篇关于发送两个ajax请求,同时在symfony中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆