python 多线程批量操作数据库测试

网上查询的资料,利用python进行多线程连接数据库,修改了部分

#!/usr/bin/env python
import thread,threading
import os,sys,time,getopt,MySQLdb

optmap = {
                'user' : 'test_lvs',
                'passwd' : 'test_lvs',
                'host' : '192.168.0.1',
                'port' : 3306,
                'db' : 'zhp',
                'use_unicode' : True,
                'charset' : 'utf8',
                'unix_socket':'/my/base/mysql.sock'
         }

db_conn=MySQLdb.connect(**optmap)
db_cursor=db_conn.cursor()

TOTAL_USERS=1111100
THREAD_NUM=16

class Run_sp(threading.Thread):
        def  __init__(self,thread_num,user_list):
                threading.Thread.__init__(self)
                self.thread_num=thread_num
                self.user_list=user_list

        def run(self):
                db_conn=MySQLdb.connect(**optmap)
                db_cursor=db_conn.cursor()
                num=0
                for user in self.user_list:
                        num+=1
                        account=user[0]
                        password=user[1]
                        #db_cursor.execute("select id,name  from userinfo where id='%s' and name ='%s'"%(account,password) )
                        db_cursor.execute("update userinfo set updation=now() where id='%s' and name ='%s'"%(account,password) )

                         db_conn.commit()

                          while 1:
                                flag= db_cursor.nextset()
                                if flag!=1:break
                                db_cursor.fetchall()


                        if num%1000==0:
                                print "Thread %d execute %d users"%(self.thread_num,num)
                print "thread %d end at [%s]"%(self.thread_num,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
                db_cursor.close()
                db_conn.close()


def main():
        user_list=[]
        begin_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        sql="select id as Account,name as Password from userinfo limit %d"%TOTAL_USERS
        db_cursor.execute(sql)
        result=db_cursor.fetchall()
        for row in result:
                account=row[0]
                password=row[1]
                user_list.append((account,password))

#       print "size of user_list:%d"%(len(user_list))
#       print user_list
#       print user_list[0:2]

        account_in_each_thread=int(TOTAL_USERS/THREAD_NUM)
#       print "account_in_each_thread : %d\n"%account_in_each_thread

        thread_list=[]
        for i in range(0,THREAD_NUM):
                if i!=THREAD_NUM-1:
                        thread_list.append(Run_sp(i,user_list[i*account_in_each_thread:(i+1)*account_in_each_thread]))
                else:
                        thread_list.append( Run_sp(i,user_list[i*account_in_each_thread:]) )
                thread_list[i].start()

        end_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        print "start at [%s]"%begin_time
        print "end at [%s]"%end_time

main()

你可能感兴趣的:(Python)