0

I have the following code:

Angular Code:

$http.get('admin/a').success(function(){
        console.log("A finish");
    });
$http.get('admin/b').success(function(){
    console.log("B finish");
});

PHP Code:

Route::group(array('prefix' => 'admin'), function()
{   
    Route::get('a', array('before' => 'auth.backend', function(){  
        // SLEEP!
        sleep(5);       
        return Response::json(array('a' => 'a'));
    }));

    Route::get('b', array('before' => 'auth.backend', function(){               
        return Response::json(array('b' => 'b'));
    }));
}

My problem is that the second http call does not start until the first http call finish, as you see the first route sleeps five seconds.

Does anybody know how fire and start this requests at the same time?

Thanks!

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
Gabriel
  • 1,890
  • 1
  • 17
  • 23

2 Answers2

2

The JS code you have is correct. Your server is actually sent the request for B immediately after the request for A (which you can verify in the network panel of your browser's dev tools) but the sleep is preventing the server from actually processing B until A is done.

This question talks about this problem with alternatives to sleep: How can I stop PHP sleep() affecting my whole PHP code?

Community
  • 1
  • 1
Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • I put the sleep function to emulate the execution of a command using shell_exec that takes much time. And the result is the same, the second request does not start until the firstone finish... – Gabriel Jan 06 '14 at 05:07
  • 1
    Yup so the real question to ask (I recommend posting a new one) is how to configure PHP to handle concurrent requests. I don't know PHP beyond what Google just taught me about sleep() ;) – Robert Levy Jan 06 '14 at 05:08
  • Thank you robert, I will investigate in that direction. – Gabriel Jan 06 '14 at 05:15
2

This behavior is due to the first call (/admin/a) holding the session lock during its sleep duration, so the second call (/admin/b) cannot proceed without the session lock.

You can unlock the session before you sleep by calling session_write_close().

In case you need to access the session in the first function after wake up, refer to this answer on how to reopen the session.

Community
  • 1
  • 1
user2172816
  • 1,200
  • 8
  • 11