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()