xmlhttp让asp实现“多线程”

王朝asp·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

看了大白菜芯写的php实现多线程,一时间觉得有用,就改了个asp版的。。呵呵,这里感谢他的思路!(http://blog.csdn.net/wapweb/archive/2004/08/16/76319.aspx)

1.原理实验

原理当然都一样,利用web服务器支持多线程,在同一页面里向服务器发多个http请求来完成我们的工作。更详细的分析,看大白菜芯写的吧。php里用Socket,asp当然用封装好了的xmlhttp了。

还是先实验一下,在一个页面里同时写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

[1] [2] [3] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航