0

I recently searched for a way to implement some unblocking Ajax requests. I found many answers including this and this that both satisfied my needs. After implementing the later solution, I tried to send an Ajax request and then calling another one so the first one aborted (so doesn't make user wait) and the second one runs.

But I come across this situation that although tools like firebug shows that the first request is aborted, but the second request execution would take as long as the time needed for execution both requests.

To make it more readable, assume that "request1" takes 10 seconds to execute and "request2" takes 5 second. Now if I abort "request1" in second 5, then "request2" will take approximately 10 seconds (5 second from "request1" and 5 from "request2") to execute.

I monitor the Apache server execution and my first guess is that when the request is sent to server, no matter we abort it or not in client-side, the server resources will be consumed and the only thing that happens is that client doesn't expect the result because the requester(request1 in my example) is aborted.

Now my question: If my guess is right, Is there anyway to let server know to stop the current process and start the next one(this question has no answer)?

Or if our server has more than processing power, how can we let the new requests execute by another processor?

here is my code for further investigation:

//Ajax Request Queue
var ajaxQueue = [];

ajaxQueue.push(//Push in Ajax Queue
                $.ajax({
                type: 'POST',
                cache: false,
                url: url,
                data: {'token': $.cookie('s3_ctoken')},
                dataType: 'html',
                beforeSend: function (xhr, setting) {

                },
                success: function (res, textStatus, xhr) {

                },
                error: function (xhr, textStatus, thrownError) {

                },
                complete: function () {

                },
                async: true
            })
            );

//On next ajax request
if(ajaxQueue.length > 0){//Means there is some ajax requests are running
 for(var i = 0; i < ajaxQueue.length; i++)
  ajaxQueue[i].abort();
}
Community
  • 1
  • 1
Sobhan Atar
  • 490
  • 1
  • 5
  • 14
  • This might help you: http://stackoverflow.com/questions/2043594/ignore-user-abort-ignored-by-php – arkascha Apr 29 '14 at 09:35
  • @arkascha This is exactly my problem (not the solution). Here I want server to stop executing script, not execute it and after that decide on sending or not sending the result to me. Based on the manual "PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client.". It means that it will execute to the end and after that it will decide not the giveback the result. Did I get it right? – Sobhan Atar Apr 29 '14 at 10:00
  • You got that right, and this is also your solution: just send a single space (that does not break anything) and flush the output buffer. That should to the trick _if_ this really is the problem you face. – arkascha Apr 29 '14 at 10:20
  • @arkascha by sending an 'space', you mean sending another ajax request to server with data:' '? If this is the case, as I stated before, my server stop responding (doesn't process anything) until the previous process has ended. So please clarify your solution a bit more. thanks in advance :) – Sobhan Atar Apr 29 '14 at 10:28
  • No, by sending a ' ' from server to client. On a regular base. Then the server side script can detect that it should abort. – arkascha Apr 29 '14 at 11:08
  • @arkascha I got it, I'll test it and will let you know – Sobhan Atar Apr 29 '14 at 11:12
  • @arkascha I tested it,even by using `flush()` and `ob_flush()` after `echo ' '`, the problem still persist and command executes to the end – Sobhan Atar Apr 30 '14 at 10:56
  • Hm, could it be that the `ignore_user_abort` flag is set somewhere? – arkascha Apr 30 '14 at 11:15
  • @arkascha Actually I checked it by `echo ignore_user_abort()` and the result is `0`, so I can say it's off and is not set. – Sobhan Atar Apr 30 '14 at 15:21
  • Strange. Maybe you are using php as cgi, not as a module as typical today? Sorry, trying to _guess_ myself, since I have no clue what's happening... – arkascha Apr 30 '14 at 15:36
  • @arkascha I'm running it on apache, is it CGI? – Sobhan Atar May 01 '14 at 01:55
  • 1
    Apache as http server can integrate php in different ways: as module, as cgi, as fast-cgi. Those alternatives all have pros and cons, but in general these days php is used as a module. Which means: apache loads a library and php is executed inside the apache child processes. cgi in contrary spawns a separate process, a dedicated php interpreter for each single request. That strategy is kind of outdated, since it is not efficient. I only mentioned it because in such cases obviously the communication between apache and php would be across process borders, so much more complex. – arkascha May 01 '14 at 10:11
  • @arkascha thanks for your detailed description and I can surely say that it's running as module under apache process – Sobhan Atar May 01 '14 at 15:13
  • Reading a bit about comments to the `abort()` method on ajax requests I have the impression that indeed the client does not wait for the results of a request, but that it does not really cancel the request. I may be wrong in this, you will have to investigate that further. But _if_ that is the case, then this issue here is not located on the server /php side, but inside ajax. Question is: is the socket really closed, cause on then php can react to that? Not sure currently how to test that though... – arkascha May 01 '14 at 15:25

0 Answers0