-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize_by_ssh_example.py
128 lines (90 loc) · 2.62 KB
/
optimize_by_ssh_example.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from multiprocessing import Process
import MySQLdb
from sshtunnel import SSHTunnelForwarder
import my_lib
import mysql_optimizer_lib
from operator import itemgetter
import time
def set_engine(table_data, connection):
engine = 'aria'
optimizer = mysql_optimizer_lib.Optimizer(
MySQLdb.connect(
**connection
))
# print(table_index)
optimizer.set_table_engine(
table_data=table_data,
engine=engine,
pack_keys=0,
transactoinal=0
)
return True
def set_compress(table_data, connection):
optimizer = mysql_optimizer_lib.Optimizer(
MySQLdb.connect(
**connection
))
optimizer.row_format(
table_data=table_data,
row_format='compressed'
)
return True
def remote_td(process_count=1):
server = SSHTunnelForwarder(
('ttt', 8150),
ssh_username="ttt",
ssh_password="",
remote_bind_address=('127.0.0.1', 3306),
# local_bind_address=('127.0.0.1', 63306),
compression=False
)
server.start()
connection = dict(
host='127.0.0.1',
port=server.local_bind_port,
db='ttt',
user='dev',
passwd='ttt',
charset="utf8mb4",
connect_timeout=30,
autocommit=True,
compress=True
)
optimizer = mysql_optimizer_lib.Optimizer(
MySQLdb.connect(
**connection
))
# optimizer.table_status_rows = sorted(optimizer.table_status_rows, key=itemgetter('ENGINE'))
list_ = optimizer.table_status_rows
procs = dict()
for index, row in enumerate(list_):
def start():
proc_ = Process(
target=set_compress,
args=(row, connection),
daemon=True
)
procs[index] = proc_
proc_.start()
def wait():
while True:
if len(procs) >= process_count or index > len(list_) - process_count - 1:
for key, value in procs.items():
if value.is_alive():
time.sleep(0.5)
else:
if value.exitcode != 0:
print('Ошибка')
return False
procs.pop(key)
return True
else:
return True
# print(len(procs), row)
start()
if wait() is False:
break
print('OK ' + row['NAME'])
server.close()
if __name__ == "__main__":
remote_td()