#coding=utf-8 import Queue,urllib,urllib2,MySQLdb import threading import time import random q = Queue.Queue(0) class MyThread(threading.Thread): def __init__(self,datalist,threadnum): self.queue=datalist self.threadnum=threadnum threading.Thread.__init__(self) def run(self): while True: if self.queue.qsize()>0: print 'length of queue:',self.queue.qsize() self.dojob(self.queue.get()) else: print 'end time:', time.ctime() break def dojob(self,data): email=data[0] password=data[1] print "threadnum:",self.threadnum,email,password requestinfo = getRequestInfo(email,password) print requestinfo time.sleep(random.random() * 3) def getDBdata(sql): conn = MySQLdb.connect(host="*******", user="****", passwd="****", db="emailresume", charset='utf8') cur = conn.cursor() cur.execute('SET NAMES UTF8') conn.commit() oper = cur.execute(sql) data = cur.fetchmany(oper) cur.close() return data def getRequestInfo(email, password): url = "http://192.168.6.80:8080/EmailResume/verify" body_value = {"email": email, "password": password} body_value = urllib.urlencode(body_value) request = urllib2.Request(url, body_value) # request.add_header(keys, headers[keys]) result = urllib2.urlopen(request).read() return result if __name__=='__main__': print 'begin time:',time.ctime() sql = "select email,password from emailstate where login='1' and resume_count>30" datalist = getDBdata(sql) print '总数:',len(datalist) for data in datalist: q.put(data) for i in range(3): MyThread(q,i).start() # print 'end time:',time.ctime()
Python多线程
最新推荐文章于 2020-12-23 13:54:40 发布