python 多线程读取数据库

from mysql import MYSQL
import threading
zzzzz = []
def run(z,zz):
    SQL = "select * from e_verify_email where Id = \'"+zz+"\'"
    my = MYSQL(SQL)
    alldata = my.getRs()
    zzzzz.append(alldata)
if __name__ == "__main__":
    threads = []
    k = 5
    for a in range(0,k):
        z = threading.Thread(target=run,args=(a,str(a+1)))
        threads.append(z)
    for a in range(0,k):
        threads[a].start()
    for a in range(0,k):
        threads[a].join()
        
    for a in zzzzz:
        print a

  

import MySQLdb
class MYSQL:
    def __init__(self,sql):
        conn = MySQLdb.connect(user='root',passwd='',db='test')
        cursor = conn.cursor()
        cursor.execute(sql)
        self.alldata = cursor.fetchall()
        cursor.close()
        conn.close()
    def getRs(self):
        return self.alldata

  

编辑器加载中...

转载于:https://www.cnblogs.com/ctwyw/archive/2011/09/02/2163931.html

你可能感兴趣的:(python 多线程读取数据库)