javascript - Parallel AJAX requests in jQuery -


my application runs 180 ajax jobs io-intensive @ server side (long running select queries).

i optimize load of multiple cpu cores have available, switching design in each ajax call executed sequentially design in these requests executed maximum of, say, 4 in parallel.

a possible, ugly solution issuing 180 requests @ same time on client , have server use semaphore stored @ session or application level. discuss application workloads later.

i find nicer solution in calls started in order (each row on table different check query) when terminates, next started , there number (namely 4) of concurrent ajax requests respective loader indicators.

i have tried use threadpool-js have found myself cannot use jquery workers

my current code following

function globalcheck() { //entry point         if (validatedate()) {             //below global variables             list = $(".chkclass:checked"); //only checked rows deal ajax request             num = $(".chkclass:checked").length; //total number of ajax calls             done = 0; //count of complete calls. when reaches num done!              if (list.length == 0) {                 alert('...');                 return;             }              $(".pmessage").fadeout();             $(".tbstatus").html('');             $(".submit").hide();             $(".exportfunctions").fadeout();             $(".loader").show();             $(":checkbox").attr('disabled', true);              singlecheck(0); //simplification, other non interesting things here         }     }   function singlecheck(index) {         avalue = $($(list).get(index)).val();          var splitted = avalue.split('_');         $('#loader_' + avalue).show();         $('#green_' + avalue).hide();         $('#yellow_' + avalue).hide();         $('#red_' + avalue).hide();         $('#print_' + avalue).hide();         $('#xls_' + avalue).hide();         $('#summ_' + avalue).hide();          $.ajax({             type: 'get',             url: '@url.action("single", "check")',             data: {                 ptype: splitted[0],                 pidquery: splitted[1],                 pdatebegin: $('#date_begin').attr('value'),                 pdateend: $('#date_end').attr('value'),                 pnow: math.floor(math.random() * 1000000)             },             success: function (data) {                 if (!checksessionexpired(data)) {                     //alert(data);                      $("#tdstatus_" + avalue).html(data);                     $("#loader_" + avalue).hide();                     done++; //done 1 more query                     $(".progress").each(function (i, crow) { $(this).html([update status]); });                      if (done == num) {    // finish?                         finishcheck();                     }                     else {                         singlecheck(done); //go next                     }                 }             },             error: function (xmlhttprequest, textstatus, errorthrown) {                 alert(errorthrown);                 redirecttoerror();             }         });     } 

result following:

auitool

question is: approach can follow in order create concurrent ajax requests in scenario?

[edit] forgot discuss application demands: application running live not serving large user base. when user submits data checked, application perform intensive operation, while staying idle long periods of time.

$.ajax() asynchronous function initiates request , returns immediately. if call multiple times in row, create concurrent requests.

your code still runs on single thread, http requests happen in parallel in background , jquery invokes callbacks return data. parallelism without having deal threads directly.

in globalcheck():

var concurrent_requests = 4;  while (done < concurrent_requests) {   singlecheck(done++); } 

will start 4 parallel requests, , existing code trigger new request each time 1 finishes, have 4 parallel requests running. , because your code runs on single thread, don't have worry concurrency issues done variable etc.

for more concurrent requests, increase value of concurrent_requests, note you'll hit browser's limit of concurrent requests single domain - varies per browser it's pretty small number. see answer specifics.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -