JQuery POST multithreaded delays

I am playing around with multithreaded POST requests in PHP code.

Since these requests can take a long time, I decided to multithreaded these POST requests in order to do something faster using multiple connections.

At this point, I wrote a scheduler in javascript that makes POST requests (using the jQuery post command) to request_helper.php and monitors if there are up to 4 pending requests at that time. In theory, I can get data 4x faster.

I started playing with request_helper.php with this content:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
sleep(20);
exit (0);
?>

      

and run 4 POST requests at the same time. and after 20 seconds I got 4 POST responses. Fine!

However, if I add start_session () - what do I need to check if the user has permissions:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
// always load or start session
session_start();
sleep(20);
exit (0);
?>

      

the first request takes 20 seconds to respond, but 40 seconds for the second, 3 seconds for the third - it looks like session_start can handle one request at a time. I am really losing multithreading.

Why is this happening, how to do it better?

+3


source to share


1 answer


You can use sessions this way, but you need to unlock the session before sending the next request.

session_write_close();



Should do it.

+1


source







All Articles