Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 views
in Technique[技术] by (71.8m points)

php - send two ajax request at the same time in symfony

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...