1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| from multiprocessing import Pool import pymysql import time import os import io def processData(txt): print('开始执行:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) command='mysql -uroot -poracle -Dtest <'+txt os.system(command) print('执行完成:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) return if __name__ == '__main__': conn = pymysql.connect("192.168.56.10", "scott", "tiger", "test") cur = conn.cursor() sql = "SELECT concat('update t set owner= ''SB'' where object_id>',avg_row * (n - 1),' and object_id<=', avg_row * n, ';') split_sql " \ "FROM (SELECT n, min_id, max_id, ceil(max_id / 100) avg_row FROM (WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM x WHERE n < 100" \ ") SELECT * FROM x) a, (SELECT min(object_id) min_id FROM t) b, (SELECT max(object_id) max_id FROM t) c ) a" cur.execute(sql) rows = [] result = cur.fetchall() slice0 = open('slice0.txt', 'w', newline='', encoding='utf8') slice1 = open('slice1.txt', 'w', newline='', encoding='utf8') slice2 = open('slice2.txt', 'w', newline='', encoding='utf8') slice3 = open('slice3.txt', 'w', newline='', encoding='utf8') for i in range(1,len(result)+1): if i%4==0: slice0.writelines(result[i - 1][0] + '\n') elif i%4==1: slice1.writelines(result[i - 1][0] + '\n') elif i%4==2: slice2.writelines(result[i - 1][0] + '\n') elif i%4==3: slice3.writelines(result[i - 1][0] + '\n') slice0.close() slice1.close() slice2.close() slice3.close() cur.close() conn.close() with Pool(4) as pool: pool.map(processData,['slice0.txt','slice1.txt','slice2.txt','slice3.txt'])
|