用ASP開"多線程"
在網(wǎng)上找到一個(gè)用ASP開的假線程,發(fā)現(xiàn)和我以前做的一個(gè)程序不謀而合,只不過以前用的是VB,摘下來,儲(chǔ)備.
1.原理實(shí)驗(yàn) 原理當(dāng)然都一樣,利用web服務(wù)器支持多線程,在同一頁面里向服務(wù)器發(fā)多個(gè)http請(qǐng)求來完成我們的工作。還是先實(shí)驗(yàn)一下,在一個(gè)頁面里同時(shí)寫2個(gè)txt文件,比較寫入時(shí)間的差異。代碼如下: <%
startime=timer()
''----------asp實(shí)現(xiàn)多線程----------''
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
if(Request.QueryString("action")="") then
runThread()
a()
else
b()
end if
%> Script Execution Time:<%=fix((timer()-startime)*1000)%>ms 運(yùn)行后的結(jié)果顯示: a文件和b文件中的時(shí)間是基本相同的。 2.實(shí)際應(yīng)用比較 比如我同時(shí)抓取2個(gè)頁面的html代碼,一個(gè)sohu首頁,一個(gè)是sina首頁,用2種方式:一個(gè)是常規(guī)的順序的代碼執(zhí)行,單線程執(zhí)行,一個(gè)是這里的多線程執(zhí)行,比較頁面完成時(shí)間,代碼如下: testspeed1.asp: <%
startime=timer()
function getHTTPPage(url)
on error resume next
dim http
set http=Server.createobject("Msxml2.XMLHTTP")
Http.open "POST",url,false
Http.send()
if Http.readystate<>4 then exit function
getHTTPPage=bytes2BSTR(Http.responseBody)
contents = getHTTPPage
Response.Write "<xmp>"
Response.Write(contents)
Response.Write "</xmp>"
set http=nothing
if err.number<>0 then err.Clear
end function
Function bytes2BSTR(vIn)
dim strReturn
dim i,ThisCharCode,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
getHTTPPage("http://www.sohu.com/")
getHTTPPage("http://www.sina.com.cn/")
%> Script Execution Time:<%=fix((timer()-startime)*1000)%>ms Testspeed2.asp: <%
startime=timer()
function getHTTPPage(url)
on error resume next
dim http
set http=Server.createobject("Msxml2.XMLHTTP")
Http.open "POST",url,false
Http.send()
if Http.readystate<>4 then exit function
getHTTPPage=bytes2BSTR(Http.responseBody)
contents = getHTTPPage
Response.Write "<xmp>"
Response.Write(contents)
Response.Write "</xmp>"
set http=nothing
if err.number<>0 then err.Clear
end function
Function bytes2BSTR(vIn)
dim strReturn
dim i,ThisCharCode,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
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()
getHTTPPage("http://www.sohu.com/")
end function
function b()
getHTTPPage("http://www.sina.com.cn/")
end function
if(Request.QueryString("action")="") then
runThread()
a()
else
b()
end if
%> Script Execution Time:<%=fix((timer()-startime)*1000)%>ms 運(yùn)行的時(shí)間結(jié)果: 次數(shù) Testspeed1運(yùn)行時(shí)間ms Testspeed2.asp運(yùn)行時(shí)間ms 1 15593 13078 2 13343 14375 3 12828 12515 4 12437 12125 5 12109 11734 6 12281 12140 7 12703 12062 8 13468 12656 9 12328 12187 10 12343 12156 以上10次是一個(gè)頁面完后另一個(gè)頁面再執(zhí)行的。誰先誰后也是任意的。有一條記錄異常。 為了避免網(wǎng)絡(luò)的原因,以下5次將測(cè)試地址改成本機(jī)http://127.0.0.1 11 109 46 12 62 46 13 62 48 14 78 64 15 62 46 以上5次是一個(gè)頁面完后另一個(gè)頁面再執(zhí)行的。誰先誰后也是任意的。 結(jié)果:好象是要快一點(diǎn)哦。。。。。。。。。。。
相關(guān)文章
ASP文章系統(tǒng)解決方案實(shí)現(xiàn)上一頁下一頁
ASP文章系統(tǒng)解決方案實(shí)現(xiàn)上一頁下一頁...2007-01-01asp Http_Referer,Server_Name和Http_Host
以前感覺Request.ServerVariables里的值很多,現(xiàn)在看看還是那么多,不過今天談其中的一個(gè)值----HTTP_Referer以及Request.ServerVariables里Server_Name與Http_Host之間有什么區(qū)別呢?2009-04-04asp中Request.ServerVariables的參數(shù)集合
這篇文章主要介紹了asp中Request.ServerVariables的參數(shù)集合,需要的朋友可以參考下2020-02-02asp 內(nèi)置對(duì)象 Application 詳解
asp 內(nèi)置對(duì)象 Application 詳解...2007-11-11