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

  

编辑器加载中...

posted @ 2011-09-02 16:24  阿奴  阅读(7694)  评论(3编辑  收藏  举报