您的位置:首页 > 编程语言 > Python开发

python多线程采集原理测试

2013-03-30 13:03 141 查看
import threading,time,random

class weburl():

def __init__(self):

self.l=[]

self.count=0

self.cururl=5

self.per=100

def getUrl(self):

if len(self.l)==0:

self.cururl=5

else:

self.cururl=int(self.l[self.count-1]) % self.per

print "cururl:" + str(self.cururl)

pass

self.count +=1

nl=[]

count = random.randint(5,7)

for i in range(1,self.cururl):

randstr=random.randint(1,1000)

nl.append(str(randstr))

return nl

def getUniqueUrl(self):

nl=self.getUrl()

for ni in nl:

try:

self.l.index(ni)

pass

except ValueError:

self.l.append(ni)

mylock = threading.RLock()

web=weburl()

class myThread(threading.Thread):

def __init__(self, name, web):

threading.Thread.__init__(self)

self.t_name = name

self.web=web

def run(self):

while True:

mylock.acquire()

self.web.getUniqueUrl()

print '\nThread(%s) locked, Number: %d'%(self.t_name, self.web.count)

if self.web.count>=len(self.web.l):

#mylock.release()

print 'len(l):'+ str(len(self.web.l))

#for i in self.web.l:

#print i

break

mylock.release()

time.sleep(0)

def test():

for i in range(1,4):

threadi=myThread('A'+str(i), web)

threadi.start()

if __name__== '__main__':

test()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: