网络编程 
首页 > 网络编程 > 浏览文章

用ASP开"多线程"

(编辑:jimmy 日期: 2024/10/8 浏览:3 次 )

在网上找到一个用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次是一个页面完后另一个页面再执行的。谁先谁后也是任意的。 结果:好象是要快一点哦。。。。。。。。。。。

上一篇:整理了一个editplus的剪辑文件(ASP方面的内容)
下一篇:使用xmlHttp结合ASP实现网页的异步调用
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?