发新话题
打印

xmlhttp让asp实现“多线程”

xmlhttp让asp实现“多线程”

看了大白菜芯写的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

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运行时间msTestspeed2.asp运行时间ms
11559313078
21334314375
31282812515
41243712125
51210911734
61228112140
71270312062
81346812656
91232812187
101234312156
以上10次是一个页面完后另一个页面再执行的。谁先谁后也是任意的。有一条记录异常。
为了避免网络的原因,以下5次将测试地址改成本机http://127.0.0.1
1110946
126246
136248
147864
156246
以上5次是一个页面完后另一个页面再执行的。谁先谁后也是任意的。

结果:好象是要快一点哦。。。。。。。。。。。


附录:阿泰兄发布了一个《XMLHTTP批量抓取远程资料》,打算和他说说,修改成这个思路的,发布出来,估计要等一下了。

TOP

发新话题