Python多线程

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值