程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> 關於ASP編程 >> 用ASP開多線程

用ASP開多線程

編輯:關於ASP編程

在網上找到一個用ASP開的假線程,發現和我以前做的一個程序不謀而合,只不過以前用的是VB,摘下來,儲備.

1.原理實驗 原理當然都一樣,利用web服務器支持多線程,在同一頁面裡向服務器發多個http請求來完成我們的工作。還是先實驗一下,在一個頁面裡同時寫2個txt文件,比較寫入時間的差異。代碼如下: <%

startime=timer()
''----------asp實現多線程----------''
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 運行後的結果顯示: a文件和b文件中的時間是基本相同的。 2.實際應用比較 比如我同時抓取2個頁面的html代碼,一個sohu首頁,一個是sina首頁,用2種方式:一個是常規的順序的代碼執行,單線程執行,一個是這裡的多線程執行,比較頁面完成時間,代碼如下: 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 運行的時間結果: 次數 Testspeed1運行時間ms Testspeed2.asp運行時間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次是一個頁面完後另一個頁面再執行的。誰先誰後也是任意的。有一條記錄異常。 為了避免網絡的原因,以下5次將測試地址改成本機http://127.0.0.1 11 109 46 12 62 46 13 62 48 14 78 64 15 62 46 以上5次是一個頁面完後另一個頁面再執行的。誰先誰後也是任意的。 結果:好象是要快一點哦。。。。。。。。。。。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved