-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple.py
83 lines (63 loc) · 2.26 KB
/
multiple.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
#-*-coding:utf-8-*-
from threading import Thread
import pymysql
from Sqlcore import Sqlcore
'''
入口功能
'''
'''vmode打印显示模式full/standard/simple'''
#本机的photo相册
conn = {"hostname":"localhost","username":"website","password":"website123","database":"photodb","hostport":3306}
cfgs = {"table":"sdb_photo", "column":"corver,gallery", "offset":0, "vmode":"simple", "limit":5, "sleep": 0}
'''开始执行核心工作'''
def runcore(uniq, conn, cfgs):
sc = Sqlcore(uniq, conn, cfgs)
sc.run()
'''结果集总数'''
def totalcount(conn, table):
db = pymysql.connect(conn['hostname'], conn['username'], conn['password'], conn['database'], conn['hostport'])
cursor = db.cursor()
cursor.execute("SELECT count(*) FROM `{table}`".format(table=table))
#cursor.fetchone()
data = cursor.fetchone()
#print(data[0])
return int(data[0])#type(data[0])
if __name__ == "__main__":
print("welcome")
total = totalcount(conn, cfgs["table"])
print("发现数据库表"+cfgs["table"]+"存在总数:"+str(total)+"条记录")
while True:
tnum = input("开启线程数,最少1个,最多10个,exit退出:")
if tnum == "exit":
exit()
if tnum.isdigit():
tnum = int(tnum)
if tnum <1:
raise ValueError("输入的必须是正整数啊")
elif tnum > 50:
print("线程数不能够超过50啊~")
elif total < tnum:
print("结果集还没有线程多~")
else:
break
else:
print("输入的必须是1以上的数字啊~")
if tnum == 1:
#单线程
runcore("ONETHREAD", conn, cfgs)
else:
#多线程
one = total//tnum #每个线程的开始游标
tlist = []
for i in range(tnum):
cfg = cfgs.copy()
cfg["offset"] = one * i
#print(cfgs)
t = Thread(target=runcore, args=("#t"+str(i), conn, cfg))
tlist.append(t)
t.start()
#print("\n" + t.getName())#获取线程名
for i in tlist:
i.join()#阻塞主线程,当前执行完再执行下一步
print("allDownloaded")