0
votes

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

for example

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

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

FireBug Screen

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

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

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

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

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

FireBug Screen

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

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

2

2 Answers

5
votes

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.

Your case is the following:

  • first request arrives, lock session
  • second reauest arrives, tries to lock session, has to wait
  • [ 5 sec sleep ]
  • first request finishes, releases lock
  • second request starts, achieves session lock
  • [ 5 sec sleep ]
  • second request finishes

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

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.

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

-2
votes

Possibly symfony does something to ajax requests. What happens if you try:

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

?