0

This is driving me nuts for a while now. I have an ajax call like so:

function update()
{
    $.get("update.php", function(data)
    {
        $("#output-progress").html(data);
    });
}

And I call it like so:

window.setInterval(function()
    {
        update();
    }, 2000);
}

Then I have another calc function which is also called:

function calc()
{
    $.get("calc.php", function(data)
    {
        //whole bunch of lines to re-render page
    });
}

So the idea is that while calc() is running, the update() will periodically update a div on the progress.

However what is happening is that if I open the console and check the calls to update() is triggered every 5 seconds, but they just stall and they complete only after the calc() has returned. I first thought this was a browser/jQuery issue, but if I log both the functions into separate log files in PHP then the update() gets logged only after the calc() finishes!

Im not sure what is going on here, any pointers are greatly appreciated!

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Undefined Variable
  • 4,196
  • 10
  • 40
  • 69
  • 2
    [**This**](http://stackoverflow.com/a/10152046/2518525) is worth a read. – Darren Jan 08 '16 at 06:20
  • This sounds like it totally depends upon your PHP scripts and infrastructure. This is likely a server thing, not a client thing. For the `update()` script to run at the same time as the `calc()` script is already running, you need scripts that support that (no contention for shared resources) and an infrastructure that will actually run multiple PHP scripts at the same time. – jfriend00 Jan 08 '16 at 06:49
  • @Darren - What does this question have to do with [that answer](http://stackoverflow.com/a/10152046/2518525)? – jfriend00 Jan 08 '16 at 06:49

1 Answers1

1

Most likely, you are using sessions, and both calc.php and update.php access session data. In order to ensure data consistency in sessions, access to session data is locked, so only one php process can access the session at a time. This means that while calc.php has the session, no other page access can read it.

What you will want to do is call session_write_close() after calc.php has finished anything that might require access to the session, and before it starts its time-consuming task.

session_write_close() writes the current session's data and releases the lock. Once calc.php is no longer holding to the session, accesses from update.php can read it.

jbafford
  • 5,528
  • 1
  • 24
  • 37