我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 网络学院 > 网络编程 > ASP专区 > Asp安全/XML > xmlhttp让asp实现“多线程”
热门文章排行
热门文章排行 手推车”功能的实现(10-07)
八大法则防范ASP网站漏洞(10-23)
ASP教程十一、调试ASP脚本(10-23)
在JSP中访问数据库大全(10-23)
虚机服务中常见Asp.Net低级错误一览(03-21)
精采文章排行
精采文章排行 ASP.NET实现抓取网页中的链接(11-15)
ASP连接数据库的11种方法(11-10)
如何动态创建网页的RSS内容摘要(11-10)
ASP网站漏洞及入侵防范方法(11-10)
ASP自定义函数:对字符串正则替换(11-10)
技术专题推荐
网管论坛交流
 

xmlhttp让asp实现“多线程” 

作者:佚名   来源:一亩三分地   点击:   日期:2007-03-22

原作者:一风一云

看了大白菜芯写的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运行时间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次是一个页面完后另一个页面再执行的。谁先谁后也是任意的。


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





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




文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·ASP中巧用Response属性 ·第六课:ASP脚本循环语句
·在 Web 页上使用条件数值格式 ·连接数据库查询手册(不仅仅适用于asp)
·警惕"给你的FileSystemObject对象加把锁" ·用ASP做全文检索

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。