Send two ajax requests at the same time to symfony

When I send two ajax requests together using jQuery .. the answer comes along

eg

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

ajax.php, ajax2.php are two files containing a cycle dummy for a cycle of about 5 seconds.

FireBug Screen

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

Here, each request takes about 5 seconds to complete .....

When I do the same example in symfony, I got a different result

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

action1, action2 are two actions that simply contain fiction for the loop, take about 5 seconds.

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

Please note that the second request executed after the completion of the first, I do not know why this happened

0
source share
2 answers

When a request arrives and it tries to start a session, php checks to see if the same session is currently in use. If so, the new request must wait until another request completes or release the session lock.

In your case, the following:

  • first request is running, session lock
  • a second reauest appears, trying to block the session, you have to wait
  • [5 seconds of sleep]
  • the first request completes, blocks the lock
  • the second request is started, the session is blocked
  • [5 seconds of sleep]
  • second request ends

By default, symfony starts a session at the beginning of each request.

In pure PHP, you can release the lock on a session file using session_write_close() . Symfony has an sfUser class that wraps session functionality, you will need to call the shutdown() method.

Please note that if you change the session data later in this request, it will not be saved.

For a more detailed explanation, read write lock in PHP Session and how to handle it in symfony .

+4
source

Perhaps symfony is doing something for ajax requests. What happens if you try:

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

?

-2
source

Source: https://habr.com/ru/post/1435958/


All Articles