Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery POST multithreaded delays

I am playing with multithreaded POST requests in PHP code.

As these requests can take a long time, I decided to multithread these POST requests to do things faster by using multiple connections.

At this moment I wrote scheduler in javascript that executes POST requests (with jQuery post command) to request_helper.php, and watch if there are up to 4 pending requests at the time. In theory I can get data 4 times faster.

I started to play with request_helper.php with that 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. Great!

However if I add start_session() - which I need for checking 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 response for first request takes 20 seconds, but for the second - 40 seconds, for the third 3 seconds - it looks like session_start could handle one request at the time. I actually lose multithreading.

Why this happens, how to do it better?

like image 945
Łukasz M Avatar asked Mar 23 '26 09:03

Łukasz M


1 Answers

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

session_write_close();

Should do it.

like image 165
Edward Avatar answered Mar 25 '26 23:03

Edward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!