I'm trying to get a progress bar showing a long process real time progress. I think I got most of if done, but I hit an interesting road block.
In a nutshell I have a button that when launched calls a JavaScript function that will do 2 things:
- launch an asynchronous Ajax call to start my long running script. This script will update a table with the progress at certain key blocks of code, so a table will have a number from 0 to 100 and some message.
- launch in a timer a synchronous call to read the database table for the progress made to update the progress bar for the user
When I launch this I noticed that (2) will wait for (1). I noticed that the call is sent (in the DeveloperTools -> Debug) but (I believe) CodeIgniter queues the second Ajax call until the first is completed.
Is there a way around this so that my (2) call goes to DB and back multiple times while (1) still executes?
And just to put some code:
function button_pressed_for_long_action(type, id)
{
//start the timer
timer = window.setInterval(get_progress, 3000);
//call the long script");
$.ajax({
url: "/the URL for long action/",
dataType: "json",
method: "POST",
data: {
type : type,
id : id
},
success:function(data)
{
},
error: function( data, status, error ) {
alert("error");
alert(error);
}
});
}
and the function called by the timer to get the progress:
function get_progress()
{
$.ajax({
url: "/url to get process/",
dataType: "json",
method: "POST",
async : false,
data: {
some_id : some_id
},
success:function(data)
{
//update UI
if (progress < 100)
{
//exit if not done
return;
}
//script is finished
window.clearInterval(timer);
},
error: function( data, status, error ) {
alert("error");
alert(error);
}
});
}
The URLs call CodeIgniter controller functions that read DB and return JSON with information correctly.
The problem is just that getting the progress (2) waits until (1) is finished.
Thanks in advance!
CodePudding user response:
after a bit of more digging I found it. PHP seems to queue requests up just as I thought. The queue is per session. So if session is closed then the lock is lifted and another call can go through.
So in my case I need to have the following line in the beginning of the long executing script:
The_long_script_controller.php
function execute_long_script()
{
//this is the line I needed:
session_write_close();
//then do the full long processing
}
All based on this thread: Two simultaneous AJAX requests won't run in parallel
This fixes my issue