1

I have a form that is validated then submitted with the following handler :

submitHandler:function (form) {


    $.ajax({
        url: 'long_process.php',
        type: 'POST',
        data: $(form).serialize(),
        success: function (result) {
            // ... Redirect ...
        }
    });


    //start polling
    (function poll() {
        setTimeout(function () {
            $.ajax({
                url: "get_progress.php",
                success: function (data) {

                    console.log(data);
                    //Setup the next poll recursively
                    poll();
                },
            });
        }, 3000);
    })();


}

long_process.php takes about 30s to finish and in the meantime I'd like to track the progress via get_progress.php which echo the percentage of processing done.

When launching this script I get in the console (edited):

1 POST long_process.php
2 GET get_process.php (3 seconds later)...
...stuck here until long_process.php finishes THEN 
3 GET get_process.php (3 seconds later)...
4 GET get_process.php (3 seconds later)...
...

but none of the get_progress.php return any values until the long_process.php is finished.

How can I achieve multiple simultaneous ajax request ? Ultimately this will be used to display a progress bar.

Fredovsky
  • 387
  • 4
  • 16
  • `get_progress.php` must be returning, because you don't set up the next one until it returns. – Barmar Feb 23 '16 at 20:32
  • yep true, my mistake. Line 3 and 4 in the console come only once `long_process.php` finishes, THEN first call of `get_process.php` returns a value and every 3 seconds thereafter again... – Fredovsky Feb 23 '16 at 20:57
  • Do you use sessions in the scripts? By default only one script can access a session at a time, and it locks the other script out. – Barmar Feb 23 '16 at 20:59
  • See http://stackoverflow.com/questions/35190439/if-call-php-page-via-ajax-that-takes-a-while-to-run-return-and-it-sets-session/35190997#35190997 for how to access the same session in multiple scripts. – Barmar Feb 23 '16 at 21:07
  • I do indeed! Just tested with removing the session in `get_process.php` and it now works ! Thanks, I was far from thinking the problem could be related to that. I have to find a way around using sessions in `get_process.php` now, but that's another problem. Regards. – Fredovsky Feb 23 '16 at 21:08

1 Answers1

6

If you are using sessions in your PHP then the one call will block the other, because PHP won't allow two requests to use the same session space at the same time. Your two requests are probably firing, but you are not getting a request to the second until the server finishes with the first.

To solve this:

Option 1: Don't use sessions in the first script that you are calling or, if you must, then unlock the session using session_write_close(). After calling this, of course, you can't write any session variables.

Option 2: If reading and writing session variables is essential then don't use session variables in the second AJAX call and don't declare a session start. Have your first script put it's status somewhere else for you to read (in a DB, a file on /tmp) and then have the second script read the status from there.

Hope that helps!

Chris
  • 76
  • 1