-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
107 lines (82 loc) · 2.94 KB
/
main.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
#!C:\Python27\python.exe
# coding: utf8
__author__ = 'yueyt'
import Queue
import threading
import time
from utils import OperDb2
from utils import OperMysql
log_queue = Queue.Queue()
class ConsumerRecords(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
# test mysql
self.db = OperMysql('xxx', 3306, 'test', 'root', 'root')
self.tabname = 'xxx'
self.commitnum = 2000
def run(self):
print '{} starting ...'.format(self.name)
start_time = time.time()
recordslist = []
record = []
num = 0
while record is not 'quit':
try:
record = self.queue.get_nowait()
if record is not 'quit' and record is not None:
recordslist.append(record)
if len(recordslist) >= self.commitnum:
self.db.insert_many_records(recordslist, self.commitnum, self.tabname)
num += len(recordslist)
recordslist = []
except Queue.Empty:
pass
if len(recordslist):
self.db.insert_many_records(recordslist, self.commitnum, self.tabname)
num += len(recordslist)
log_queue.put([self.name, num, '{0:.6f}'.format(time.time() - start_time),
'{0:.6f}'.format(num / (time.time() - start_time))])
# self.db.close_conn()
print '{} ending ...'.format(self.name)
def run1(self):
while not self.queue.empty():
record = self.queue.get()
self.db.insert_record(self.tabname, record)
print 'thread done ...'
def ProducerRecords():
allnumber = 10000 * 100 * 2
tbname = 'xxx'
workers = []
thread_count = 3
queue = Queue.Queue()
start_time = time.time()
query_db = OperDb2('xxx', 52000, 'newdb2', 'xxx', 'xxx')
# start thread
for i in xrange(thread_count):
workers.append(ConsumerRecords(queue))
for i in xrange(thread_count):
workers[i].start()
# get source datalist
with file(r'getRec.sql') as f:
query_sql = f.read() % (tbname, allnumber)
query_db.get_source_data(query_sql, queue)
query_db.close_conn()
# put quit flag
for i in xrange(thread_count):
queue.put('quit')
# wait all thread exit
for i in xrange(thread_count):
workers[i].join()
# print log & performance
commit_num = 0
while not log_queue.empty():
tid, num, taken, tps = log_queue.get()
print 'thread[{:10s}], num[{:6d}], taken[{:10s}], tps[{:10s}]'.format(tid, num, taken, tps)
commit_num += int(num)
print '-' * 50
print 'Done {} time taken: {:0.6f}, avg TPS: {:0.6f}'.format(commit_num, time.time() - start_time,
commit_num / (time.time() - start_time))
# main
if __name__ == '__main__':
ProducerRecords()