/    Sign up×
Community /Pin to ProfileBookmark

Hello All,

I’m trying to run few requests in parallel – to save time.
Apparently even though I use async requests, and even though looks like it’s indeed async (later lines of code are being executed BEFORE results are returned) – it doesn’t really run in parallel.
When I look at the webserver’s log, I see that only AFTER I get response for the first request, the next request is sent.

Am I doing something wrong? or is it a known limitation?

Here’s my code:

[code=html]
<script type=”text/javascript”>
var str = new String();

function ajax(url){
if (window.XMLHttpRequest){
var obj = new XMLHttpRequest();
}else{
var obj = new ActiveXObject(“MSXML2.XMLHTTP.3.0”);
}
obj.open(“GET”, url, true);
obj.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
obj.onreadystatechange = function(){
if (obj.readyState == 4 && obj.status == 200) {
if (obj.responseText){
alert(obj.responseText);
}
}
}
obj.send(null);
}
ajax(“/ajax_in.php”);
ajax(“/ajax_in.php”);
ajax(“/ajax_in.php”);
ajax(“/ajax_in.php”);
</script>
[/code]

ajax_in.php:

[code=php]
<?
sleep(rand(1,10)); // Random sleep
print date(“H:i:s”); // Print current time; if ajax was parallel I’d get it unsorted.
?>
[/code]

Thanks in advance!

  • Oren
  • to post a comment
    JavaScript

    11 Comments(s)

    Copy linkTweet thisAlerts:
    @DetectAug 01.2007 — Google is MY friend, you bastard!! ?

    Google is your friend[/QUOTE]
    Copy linkTweet thisAlerts:
    @SmackwareAug 01.2007 — Hi Oren ?

    I did not find an error in your code (except you declare obj in the wrong scope)

    I have performed the following test (notice changes in ajax_in.php)
    [CODE]
    function ajax(url){
    // First off, obj should be declared out of "if" scope

    var obj;

    if (window.XMLHttpRequest){
    obj = new XMLHttpRequest();
    }else{
    obj = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    }
    obj.open("GET", url, true);

    obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    obj.onreadystatechange = function(){
    if (obj.readyState == 4) {
    if (obj.responseText){
    alert(obj.responseText);
    }
    }
    }
    obj.send(null);
    }

    ajax("ajax_in.php?a=1");
    ajax("ajax_in.php?a=2");
    ajax("ajax_in.php?a=3");
    ajax("ajax_in.php?a=4");
    [/CODE]


    ajax_in.php (now accepts parameter)
    [CODE]
    <?php
    sleep(rand(1,4)); // Random sleep
    print $_GET['a']; // Print current time; if ajax was parallel I'd get it unsorted.
    ?>
    [/CODE]



    I did not check the web logs... It is obvious the calls were sent one after the other, because the code
    [CODE]
    ajax("ajax_in.php?a=1");
    ajax("ajax_in.php?a=2");
    ajax("ajax_in.php?a=3");
    ajax("ajax_in.php?a=4");
    [/CODE]

    Is not async...

    BUT...

    the messages were in the order of 2,1,4,3... that means the php scripts were handled in an async. manner.

    Dunno what is wrong with your code... should work.

    BTW: in your ajax_in.php ... don't you want to save the date before you randomly sleep and then display what you saved after the sleep? That would indicate when the php was called.


    Oh, and by the way:

    Google is everyones friend (and my concubine).. and, IMHO, mentioning it just isn't worth a forum post.
    Copy linkTweet thisAlerts:
    @steveaAug 01.2007 — The article I linked to was found from a 5 minute Google search and was directly related to the question being asked, therefore I deemed that it did warrant a forum post. I can see that you did your homework to understand the problem and provide an articulate solution and analysis, nonetheless if you're going to say that my post was worthless you should at least have checked the link.
    Copy linkTweet thisAlerts:
    @SmackwareAug 02.2007 — The article I linked to was found from a 5 minute Google search and was directly related to the question being asked, therefore I deemed that it did warrant a forum post. I can see that you did your homework to understand the problem and provide an articulate solution and analysis, nonetheless if you're going to say that my post was worthless you should at least have checked the link.[/QUOTE]

    My apologies. Its my fault for not noticing it was a link.
    Copy linkTweet thisAlerts:
    @Oren_HeldauthorAug 02.2007 — Well - first thanks for your replies and links!

    Stevea's link mentioned that MSIE allows up to two HTTP connections per specific server. Some research showed that HTTP 1.1 limits the browser to up to two connections.

    Apparently my test showed that MSIE has up to two connections indeed, but FF didn't make more than one connection! (Maybe it counts the main page + AJAX request as two...)

    Anyway a small research showed that it can be tweaked this way (NOTE: it breaks HTTP 1.1 rules!):
    [LIST]
  • [*]Firefox: about:config -> network.http.max-persistent-connections-per-server

  • [*]MSIE: HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet SettingsMaxConnectionsPerServer (create it even if it doesn’t exist, DWORD value)

  • [/LIST]
    Copy linkTweet thisAlerts:
    @phingersOct 01.2009 — I have this same issue, tweaking firefox or ie settings did not fix it.

    Doing a simple $.ajax call with jquery 10 times.

    I get results back in a weird order

    3

    2

    1

    4

    7

    5

    etc...

    But still in serial not parallel.

    Any ideas?
    Copy linkTweet thisAlerts:
    @slaughtersOct 01.2009 — Google is MY friend, you bastard!! ?[/QUOTE]Google is nobody's friend when pointing to links that are over 2 years old ?
    Copy linkTweet thisAlerts:
    @steveaOct 01.2009 — Google is nobody's friend when pointing to links that are over 2 years old ?[/QUOTE]

    Unless of course the post itself is from two years ago, but who can be bothered to check the post date? ?
    Copy linkTweet thisAlerts:
    @phingersOct 01.2009 — I appreciate the prompt attention to the thread, and understand it is an old thread, but it seems like the only thread I can find that explains my exact issue.

    jquery ajax does not seem to be true parallel processing, the js continues to run fast, but it seems to only be sending requests to the apache server after the previous one has completed.

    I'm going to try with pure ajax instead of jquery, see if that does it. But was hoping someone had a solution.

    Thanks.
    Copy linkTweet thisAlerts:
    @dch3Oct 01.2009 — With friends like Google who needs AltaVista.
    ×

    Success!

    Help @Oren_Held spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...