多线程之查询数据库

import pymysql
import csv
import os
import threading
import time

db_config = {
            'host':host,
            'port':port,
            'user':'root',
            'password':password,
            'db':'code_data',
            'charset':'utf8',
            'cursorclass':pymysql.cursors.Cursor,

            }

def read_file_db(file):
    db_conn = pymysql.connect(**db_config)
    cur = db_conn.cursor()
    with open(file,'r') as f:
        reader = csv.reader(f,delimiter='\t')
        for row in reader:
            cur.execute("SELECT ... FROM ... WHERE ... = '%s' " % row[0][:8])
            data = cur.fetchall()
            result = [str(row[0][:8])]
            for record in data:
                result.append(record[0].replace('\n', '') + '---' + record[1].replace('\n', ''))
            # 将结果写入到new_row中
            [result.append('') for x in range(6-len(result))]
            new_row.append(result)
    # 一个文件处理完之后,就断开数据库的游标和connect连接
    cur.close()
    db_conn.close()

if __name__ == '__main__':
    start_time = time.time()
    dir = os.path.join(os.path.split(__file__)[0], 'data')
    all_file, new_row, td_list = [], [], []
    for file in os.listdir('data'):
        all_file.append(os.path.join(dir, file))
    # 开启线程,每一个线程操作一个文件
    for i in range(len(all_file)):
        td = threading.Thread(target=read_file_db, args=(all_file[i],))
        td.start()
        td_list.append(td)

    for td in td_list:
        td.join()

    # 将数据写入到文件中
    with open('result.csv','w', newline='') as f:
        writer = csv.writer(f,delimiter='\t')
        for row in new_row:
            writer.writerow(row)
    print('多线程数据库查询共花费时间: ', time.time()-start_time)

你可能感兴趣的:(多线程之查询数据库)