We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to handle requests asynchronously

I have a webpage including 3 ajax GET requests calling when openning the page.

The 3 requests refer to the same controller/view while the parameters are totally different. It means that most of the php code for the 3 requests are different. And in every request MYSQL query is needed.

In the browser developer tool, I found that the 3 requests cannot be handled asynchronously. I don't know what factors block them. This problem happens both in Chrome and Firefox, hence it may not be a browser problem.

To make sure that it is a background problem. Three "sleep(3);" have been added to 3 parts of the code (refered to the 3 requests) in the view file. From the browser, the waiting times are 3.xx, 6.xx and 9.xx seconds. Moreover, I open two browsers to open this page at the same time. The waiting time for the two browsers are both 3.xx, 6.xx, 9.xx, but not one browser are 3.xx, 6.xx, 9.xx while the another's are 12.xx, 15.xx, 18.xx.

I don't know whether the causes are from Phalcon or Apache settings.

Could any one help me solve this problem, thanks!



85.5k

do you use jquery ?



6.9k

Yes, the ajax requests are called using jquery

do you use jquery ?



85.5k
edited Oct '15
var d1 = $.Deferred();
var d2 = $.Deferred();

function call_one(){
    //ajax goes here

    $.getJSON('MY URL', params, function(responce){
        d1.resolve();
    });
}

function call_two(){
    //ajax here
    $.getJSON('MY SECOND URL / params whatever ', params, function(responce){
        d2.resolve();
    });
}

function ajax_tree(){
    //ajax here

    // since its the final one call it and do what you gotta do
}

$.when( d1 ).done(function ( data ) {
    call_two(); //since first is done lets go for the second one
});

$.when( d2 ).done(function ( data ) {
    ajax_tree(); //call the final one
});

call_one(); //call the first ajax


85.5k

did it work ?

Well, i just wonder why are you doing 3 requests to same controller/view ?



6.9k

I want to make the requests being handled asynchonnously to speed up the page loading time. The JS code above is to send requests Synchronously?

did it work ?



6.9k

Because there are almost 10 types of ajax requests in this page. So I put them in one controller/view and differ them with parameters.

Well, i just wonder why are you doing 3 requests to same controller/view ?



85.5k

my bad i was very sleepy yesturday and i was lazy to read carefully :D

https://www.browserscope.org/?category=network

dont forget that your css js and so on are still calls to the server. User can change this values from their computers, but you can't change it from the code.



6.9k

Yes, I think it's the same issue. But I haven't found any config setting example to solve the problem. Could you please help me on this for some details? Thanks!

My environment is XAMPP for MAC.

I only found this page https://php.net/manual/es/memcached.sessions.php. Some one had mentioned the parameters (MEMC_SESS_LOCK_ATTEMPTS MEMC_SESS_LOCK_WAIT MEMC_SESS_LOCK_EXPIRATION). It may helps but there is no details.

It will probably be something like this: https://stackoverflow.com/questions/10094348/concurrent-connections-with-apache-coming-from-same-client