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()
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()
相关文章推荐
- proquest的采集程序demo测试截图
- 利用DHT网络原理制作bt采集蜘蛛,开源版
- Flink架构、原理与部署测试
- 基于MTCNN的人脸自动对齐技术原理及其Tensorflow实现测试
- 各种流量采集方式的性能对比测试结果
- 利用DHT网络原理制作bt采集蜘蛛
- 性能测试知多少---性能测试工具原理与架构
- Framebuffer原理、使用、测试系列文章
- 利用DHT网络原理制作bt采集蜘蛛,开源版
- 利用DHT网络原理制作bt采集蜘蛛
- 数字万用表二极管档测试原理与使用方法--平顶山焦化公司王水成1999年
- Android性能测试工具原理
- TDR测试原理
- 弱网测试原理以及方法(三)-使用fiddle进行弱网测试
- Flume NG安装部署及数据采集测试
- 固态硬盘掉电保护测试原理及要点
- cyclictest 测试以及原理浅谈
- 《系统程序员成长计划》读书笔记@变参函数实现原理测试用例
- darkstar 线程互斥原理以及测试!