当前位置: 移动技术网 > IT编程>开发语言>Asp > 用ASP开"多线程"

用ASP开"多线程"

2017年12月12日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网