XMLHTTP allows ASP to implement "multi-thread"

xiaoxiao2021-03-06  111

I saw the PHP of the Chinese cabbage core to implement multi-thread. For a time, it feels useful to change an ASP version. . Oh, thank you here! (Http://blog.9cbs.net/wapweb/archive/2004/08/16/76319.aspx)

Principle experiment

The principles are of course the same, using the web server to support multithreading, send multiple HTTP requests to the server to the server to complete our work. More detailed analysis, watch the big cabbage core. In the PHP, the ASP is of course XMLHTTP with a package.

Still first experiment, write 2 TXT files at the same time, compare the difference in write time. code show as below:

<%

Startime = Timer ()

'' ---------- ASP implements multi-thread --------- ''

Function Runthread ()

DIM HTTP

SET http = server.createObject ("msxml2.xmlhttp")

Http.open "get", "http://127.0.0.1/thread.asp?action=b", FALSE

Http.send ()

END FUNCTION

Function a ()

DIM Content, Filepath, Myfile

Content = now () & chr (30) & Timer ()

Filepath = server.mappath ("a.txt")

SET FSO = CreateObject ("scripting.filesystemObject")

Set myfile = fso.createtextfile (filepath, true)

MyFile.Write (Content)

Myfile.close

END FUNCTION

Function B ()

DIM Content, Filepath, Myfile

Content = now () & chr (30) & Timer ()

FilePath = Server.mappath ("B.TXT")

SET FSO = CreateObject ("scripting.filesystemObject")

Set myfile = fso.createtextfile (filepath, true)

MyFile.Write (Content)

Myfile.close

END FUNCTION

Request.QueryString ("Action") = "") THEN

Runthread ()

a ()

Else

b ()

END IF

%>

Script Execution Time: <% = FIX ((Timer () - STARTIME) * 1000)%> MS

The result of the run shows:

The time in the A file and the B file is basically the same.

2. Actual application comparison

For example, I also grabbed the HTML code of 2 pages, a SOHU home, one is the SINA home page, use 2 ways: one is the regular order code execution, single thread execution, one is the multi-thread execution here, the comparison page is completed Time, the code is as follows:

Testspeed1.asp:

<%

Startime = Timer ()

Function gethttppage (URL)

ON Error ResMe next

DIM HTTP

Set http = server.createObject ("msxml2.xmlhttp") http.open "pos", URL, FALSE

Http.send ()

IF http.readystate <> 4 THEN EXIT FUNCTION

geternal = BYTES2BSTR (http.responsebody)

Contents = gethttppage

Response.write "

"</p> <p>Response.write (Contents)</p> <p>Response.write "</ xmp>"</p> <p>Set http = Nothing</p> <p>if Err.Number <> 0 Then Err.Clear</p> <p>END FUNCTION</p> <p>Function Bytes2bstr (VIN)</p> <p>DIM STRRETURN</p> <p>Dim I, thischarcode, Nextcharcode</p> <p>Strreturn = ""</p> <p>For i = 1 to lenb (vin)</p> <p>Thischarcode = ASCB (MIDB (Vin, I, 1))</p> <p>IF thischarcode <& h80 then</p> <p>Strreturn = strreturn & chr (thischarcode)</p> <p>Else</p> <p>Nextcharcode = ASCB (MIDB (VIN, I 1, 1))</p> <p>Strreturn = strreturn & chr (thischarcode) * & H100 cint (nextcharcode))</p> <p>i = i 1</p> <p>END IF</p> <p>NEXT</p> <p>BYTES2BSTR = STRRETURN</p> <p>END FUNCTION</p> <p>Gethttppage ("http://www.sohu.com/")</p> <p>gethttppage ("http://www.sina.com.cn/")</p> <p>%></p> <p>Script Execution Time: <% = FIX ((Timer () - STARTIME) * 1000)%> MS</p> <p>Testspeed2.asp:</p> <p><%</p> <p>Startime = Timer ()</p> <p>Function gethttppage (URL)</p> <p>ON Error ResMe next</p> <p>DIM HTTP</p> <p>SET http = server.createObject ("msxml2.xmlhttp")</p> <p>HTTP.Open "Post", URL, FALSE</p> <p>Http.send ()</p> <p>IF http.readystate <> 4 THEN EXIT FUNCTION</p> <p>geternal = BYTES2BSTR (http.responsebody)</p> <p>Contents = gethttppage</p> <p>Response.write "<XMP>"</p> <p>Response.write (Contents)</p> <p>Response.write "</ xmp>"</p> <p>Set http = Nothing</p> <p>if Err.Number <> 0 Then Err.Clear</p> <p>END FUNCTION</p> <p>Function Bytes2bstr (VIN)</p> <p>DIM STRRETURN</p> <p>Dim I, thischarcode, Nextcharcode</p> <p>Strreturn = ""</p> <p>For i = 1 to lenb (vin)</p> <p>Thischarcode = ASCB (MIDB (Vin, I, 1))</p> <p>IF thischarcode <& h80 trenstrreturn = Strreturn & Chr (thischarcode)</p> <p>Else</p> <p>Nextcharcode = ASCB (MIDB (VIN, I 1, 1))</p> <p>Strreturn = strreturn & chr (thischarcode) * & H100 cint (nextcharcode))</p> <p>i = i 1</p> <p>END IF</p> <p>NEXT</p> <p>BYTES2BSTR = STRRETURN</p> <p>END FUNCTION</p> <p>Function Runthread ()</p> <p>DIM HTTP</p> <p>SET http = server.createObject ("msxml2.xmlhttp")</p> <p>Http.open "get", "http://127.0.0.1/thread.asp?action=b", FALSE</p> <p>Http.send ()</p> <p>END FUNCTION</p> <p>Function a ()</p> <p>Gethttppage ("http://www.sohu.com/")</p> <p>END FUNCTION</p> <p>Function B ()</p> <p>gethttppage ("http://www.sina.com.cn/")</p> <p>END FUNCTION</p> <p>Request.QueryString ("Action") = "") THEN</p> <p>Runthread ()</p> <p>a ()</p> <p>Else</p> <p>b ()</p> <p>END IF</p> <p>%></p> <p>Script Execution Time: <% = FIX ((Timer () - STARTIME) * 1000)%> MS</p> <p>Running time results:</p> <p>Testspeed1 running time MS testspeed2.asp runtime MS 1 15593 13078 2 13343 14375 3 12828 12515 4 12434 12125 5 12109 11734 6 12281 12140 7 12703 12062 8 13468 12656 9 12328 12187 10 12343 12156 The above 10 times is another page after another One page is executed. Whoever is also arbitrary after anyone. There is a record exception. In order to avoid the network, the following 5 times will test address change cost http://127.0.0.1 11 109 46 12 62 46 13 62 48 14 78 64 15 62 46 or more 5 times is another page after another page implemented. Whoever is also arbitrary after anyone.</p> <p>Result: It seems to be a little bit. . . . . . . . . . .</p> <p>Appendix: Attai brother released a "XMLHTTP bulk to grab remote information", intended to talk to him, modify this idea, release it, estimate is to wait.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-100725.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="100725" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.043</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'zXaUrYtYZHlgqdg7tGsYVeluyg1UlO_2F5Vts6PDuWBbMjSRTXeBc79CtAgu1KCoEtxDG3aimH7nVmT_2FaQSZiPQw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>