多线程求助

luotuo512 2009-08-30 08:27:10
高手来看看为什么会有错误。
# -*- coding: utf-8 -*-
#统计csdn技术专家男女比例
import urllib,re,thread,sets
male=0
female=0
mylock=thread.allocate_lock()

def tongji(m,n):
global male
global female
for i in range(m,n):
sock=urllib.urlopen("http://hi.csdn.net/RankingStaticPage/True/3/5001/%d.htm"%i)
source=sock.read()
namepattern=re.compile("a href='/\w*/profile")
link=namepattern.findall(source)
link=list(set(link))
for j in link:
sock=urllib.urlopen("http://hi.csdn.net/"+j[9:])
source=sock.read()
mylock.acquire()
if source.find("他的博客")!=-1:
male+=1
mylock.release()
elif source.find("她的博客")!=-1:
female+=1
mylock.release()


def test():
thread.start_new_thread(tongji,(0,1))
thread.start_new_thread(tongji,(1,2))
thread.start_new_thread(tongji,(2,3))
thread.start_new_thread(tongji,(3,4))

if __name__== '__main__':
test()
print male,female
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
DarkChampion 2009-08-30
  • 打赏
  • 举报
回复
过奖了

我把代码写在博客中了,lz可以拿去改一下
http://blog.csdn.net/DarkChampion/archive/2009/08/30/4500574.aspx
luotuo512 2009-08-30
  • 打赏
  • 举报
回复
DarkChampion你真是太好了,向DarkChampion致敬!!好好学习下。
你那里的速度好快啊。我处理一页要用几分钟呢。
DarkChampion 2009-08-30
  • 打赏
  • 举报
回复
改进版本:
# -*- coding: utf-8 -*-
#统计csdn技术专家男女比例
import time
import urllib,re,threading,sets

class thr_tongji(threading.Thread):
def __init__(self, threadname, m, n):
threading.Thread.__init__(self,name=threadname)
self.m = m
self.n = n
self.male = 0
self.female = 0

def run(self):
print self.getName(),self.m,self.n
for i in range(self.m,self.n):
sock=urllib.urlopen("http://hi.csdn.net/RankingStaticPage/True/3/5001/%d.htm"%i)
source=sock.read()
namepattern=re.compile("a href='/\w*/profile")
link=namepattern.findall(source)
link=list(set(link))
for j in link:
sock=urllib.urlopen("http://hi.csdn.net/"+j[9:])
source=sock.read()
#mylock.acquire()
if source.find("他的博客")!=-1:
self.male+=1
elif source.find("她的博客")!=-1:
self.female+=1
#mylock.release()

def output(self):
print self.getName(),
print self.male, self.female


def test():
start = 0
stop = 4
step = 1
thrs = []
for t in range(start, stop, step):
name = 'thr_' + str(t / step)
thrs.append(thr_tongji(name, t, t+step))
print "start time:%s"%(time.ctime())
for t in thrs:
t.start()
for t in thrs:
t.join()
print "stop time:%s"%(time.ctime())
for t in thrs:
t.output()


if __name__== '__main__':
test()


输出:
start time:Sun Aug 30 21:25:41 2009
thr_0 0 1
thr_1 1 2
thr_2 2 3
thr_3 3 4
stop time:Sun Aug 30 21:26:20 2009
thr_0 14 1
thr_1 15 0
thr_2 15 0
thr_3 14 1
DarkChampion 2009-08-30
  • 打赏
  • 举报
回复
我一般用threading

thread没用过

----

最后结果没有加起来
t1 14 1
t2 15 0
DarkChampion 2009-08-30
  • 打赏
  • 举报
回复
我用lz的代码的,lz可以参考一下
# -*- coding: utf-8 -*-
#统计csdn技术专家男女比例
import time
import urllib,re,threading,sets
male=0
female=0

class thr_tongji(threading.Thread):
def __init__(self, threadname, m, n):
threading.Thread.__init__(self,name=threadname)
self.m = m
self.n = n
self.male = 0
self.female = 0

def run(self):
print self.getName(),self.m,self.n
for i in range(self.m,self.n):
sock=urllib.urlopen("http://hi.csdn.net/RankingStaticPage/True/3/5001/%d.htm"%i)
source=sock.read()
namepattern=re.compile("a href='/\w*/profile")
link=namepattern.findall(source)
link=list(set(link))
for j in link:
sock=urllib.urlopen("http://hi.csdn.net/"+j[9:])
source=sock.read()
#mylock.acquire()
if source.find("他的博客")!=-1:
self.male+=1
elif source.find("她的博客")!=-1:
self.female+=1
#mylock.release()

def output(self):
print self.getName(),
print self.male, self.female


def test():
t1=thr_tongji('t1', 0, 1)
t2=thr_tongji('t2', 1, 2)
t1.start()
t2.start()
t1.join()
t2.join()
t1.output()
t2.output()


if __name__== '__main__':
test()

37,722

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧