Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concurrent HttpRequests

Status
Not open for further replies.

simonB2007

Programmer
Jun 12, 2007
16
0
0
GB
K,
So I want to place 2 HTTPRequests simultaneiously.
Both will return recordsets from SQL Server.

So far, I've created 2 seperate request objects, the first with a call that normally takes around 20 seconds, the second with a call that normally takes around 2 seconds.

Using Firebug COnsole in FF, I can see both get initiated and start whirring away at the same time.
However, the '2 second' call does not finish until after call number 1 has finished.
With some logging in my Stored Procedure, I can see that the second call does not even begin to execute on the SQLServer until the first has finished.

I can think of some practicalities as to why simultaneous calls might not be 'allowed' by design .. for a start, it would lead into the realms of Javascript multithreading ?

However, personally, I wouldn't mind if the second call executed, gathered it's data and THEN waited until the first had finished, at least then, both could dumping their results to the client within milliseconds of each other.
It is annoying at the moment that the second does not even start to execute, in real terms, until the first has completed.

Wondered if anyone could enlighted me on the internal workings of the browser when calling 2 HTTPRequests at the same time ?

Many Thanks
Simon


 
Hi

Other then making the requests asynchronous, I know no other thing you have to do in JavaScript. I would debug the server-side instead, starting with the web server's logs and status report if available.

Feherke.
 
So I guess you are saying that it 'should work' ?
I'll try and get hold of webserver logs as suggested.

While you're there, would you know how the client/browser would handle 2 seperate returns?
.. as in theory this would necessitate either multi-threading capabilities, or a holding-stack of some variety ?


Many Thanks
 
Hi

Simon said:
So I guess you are saying that it 'should work' ?
I can not say that, because you posted no code. But my test worked so far.
Simon said:
would you know how the client/browser would handle 2 seperate returns?
For example, like setting the [tt]src[/tt] of 2 separate [tt]Image[/tt]object : each does its job and none of them cares about the other. Of course, supposing this :
Simon said:
I've created 2 seperate request objects
means those are indeed 2 separate objects.

Note that browser settings may prevent/limit parallel requests. See [tt]network.http.pipelining[/tt] and related settings in your FireFox's about:config.

Feherke.
 
Sorry, my original code is rather complex, but have put together this simple script with which to test.


<SCRIPT>
if (window.XMLHttpRequest){xhttp=new XMLHttpRequest();}
xhttp.open("GET","localreceivers/db_get_A.asp",true);
xhttp.send("");
xmlDoc=xhttp.responseXML;


if (window.XMLHttpRequest){xhttp_2=new XMLHttpRequest();}
xhttp_2.open("GET","localreceivers/db_get_B.asp",true);
xhttp_2.send("");
xmlDoc=xhttp_2.responseXML;

</SCRIPT>
 
Hi

Simon said:
if (window.XMLHttpRequest){xhttp=new XMLHttpRequest();}
xhttp.open("GET","localreceivers/db_get_A.asp",true);
This makes me think to the anti-example in an old magazine :
Code:
100 [b]IF[/b] c=0 [b]THEN[/b]
110   [b]PRINT[/b] [i]"That's bad"[/i]
120 [b]ENDIF[/b]
130 [b]LET[/b] a=b/c
You are instantiating the xhttp object only if XMLHttpRequest exists, then you are calling xhttps's methods no matter if the previous test was successful or not.

Anyway, your code is conceptually wrong.
JavaScript:
[b]var[/b] xhttp[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]XMLHttpRequest[/color][teal]()[/teal]
[b]var[/b] xhttp_2[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]XMLHttpRequest[/color][teal]()[/teal]

xhttp[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal][green][i]'GET'[/i][/green][teal],[/teal][green][i]'localreceivers/db_get_A.asp'[/i][/green][teal],[/teal][b]true[/b][teal])[/teal]
xhttp_2[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal][green][i]'GET'[/i][/green][teal],[/teal][green][i]'localreceivers/db_get_B.asp'[/i][/green][teal],[/teal][b]true[/b][teal])[/teal]

xhttp[teal].[/teal]onreadystatechange[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal] [b]if[/b] [teal]([/teal]xhttp[teal].[/teal]readyState[teal]==[/teal][purple]4[/purple] [teal]&&[/teal] xhttp[teal].[/teal]status[teal]==[/teal][purple]200[/purple][teal])[/teal] xmlDoc[teal]=[/teal]xhttp[teal].[/teal]responseXML [teal]}[/teal]
xhttp_2[teal].[/teal]onreadystatechange[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal] [b]if[/b] [teal]([/teal]xhttp_2[teal].[/teal]readyState[teal]==[/teal][purple]4[/purple] [teal]&&[/teal] xhttp_2[teal].[/teal]status[teal]==[/teal][purple]200[/purple][teal])[/teal] xmlDoc[teal]=[/teal]xhttp_2[teal].[/teal]responseXML [teal]}[/teal]

xhttp[teal].[/teal][COLOR=darkgoldenrod]send[/color][teal]([/teal][b]null[/b][teal])[/teal]
xhttp_2[teal].[/teal][COLOR=darkgoldenrod]send[/color][teal]([/teal][b]null[/b][teal])[/teal]

Feherke.
 
erm .. how to say ..

If i read correctly, you are offering well meaning advice.
However, as i stated, this is not the actual code used my app.
The code I posted was simple rough and ready to demonstrate a particular problem.

At the end of the day, the code posted works for the purpose it was intended.

That purpose being to invesitgate why, when initiating 2 requests, the second waits for the first to finish.
I have run my code in FF, using firebug, and this 'waiting' can be observed using the console.

I do not believe, with respect, that your response answers any of the original question ?



 
[0] I believe the simplified/suggestive xml_doc should be differentiated in two separate variables (?) xml_doc and xml_doc_2 separately holding the return object. But that's not essential to the question(s).

[1] The common browser supports multiple xmlhttprequest acting concurrently-that's beyond doubt. But, the sockets available for the purpose are usally very limited in number, around 2 to 5 or so to my understanding. If the question is on two independent asynchronous requests, it should be within the limit. Hence, that would not be the holding up of the asynchroneity.

[2] The main uncertainty is that there is a very much real unpredictability on the order of completion of the requests in action. Even with a factor of the order of 10^3 expected difference, the order is still apparently stochastic, surely depending on the use of resource available to the user-agent or the irrationality of the repartition of resources for each request. Hence, it is not surprising many times the first initiated xmlhttprequest object "getting" a expected long processing time to complete the request finishing first whereas the second initiated request "getting" a very small expected processing time to complete the request finishing "surprisingly" the second only.

[2.1] You can verify this fact very easily by writing the (new Date()).valueOf() to a container (div say) from within the callback handler. Repeating the request a good number of times may show sometime the first finishing first, sometimes the second finishing first.

[2.2] As to the showing on 1 request only in the debugger of http-monitor type, I cannot confirm if the requests are done properly. It should show the correct number (if it is a small enough enough as mentioned in [1], around 2 to 5).
 
Many thanks all.

This tells me that the problem is not at the client end, but likely web-server.

I will take the knowledge you've shared and go talk to the server admins.

Many thanks, you've no doubt saved me from hours of looking the wrong place !

Regards
 
My post essentially meant it is client-side, no server-side. But you may be more inspired by the posts other than mine-I have no comment.
 
Hope you guys are still out there .. i have development! :)

Not necessarily good, mind you.

I have found that if I clear my cookies, i get parallel execution, exaclty what i want.

But if execute the code again, I'm back to the queuing scenario.
Only by going into FF Tools > Clear Private Data > Cookies, will it return to parallel execution.

I have no code messing with cookies, either in the test page or the two feeding pages.

Does this 'cookie effect' mean anything to anyone ?

Many Thanks if you are still with me on this one !
 
Hi

Hmm... I could imagine a situation :
[ul]
[li]Your cookie is storing a session ID.[/li]
[li]Both db_get_A.asp and db_get_B.asp are using session data.[/li]
[li]Session data is stored in files.[/li]
[li]Files storing the session data are locked when opened.[/li]
[li]Files storing the session data are kept open while the ASP script is running.[/li]
[/ul]
So try to either :
[ul]
[li]No use session data in db_get_A.asp or db_get_B.asp.[/li]
[li]Force closing the session file as soon as possible, after copying the needed data into local variables.[/li]
[/ul]
Note that I know nothing about ASP. For details you will have to ask in forum333 or forum855.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top