-
Notifications
You must be signed in to change notification settings - Fork 3
/
multi_insert.py
68 lines (62 loc) · 2.14 KB
/
multi_insert.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
#!/usr/bin/python
# -*- coding: utf8 -*-
import pymysql
import time
import datetime
import subprocess
from multiprocessing import Process
class Fandb:
def __init__(self, host, port, user, password, db, charset='utf8mb4', local_infile=True):
self.host = host
self.port = int(port)
self.user = user
self.password = password
self.db = db
self.charset = charset
self.local_infile = local_infile=True
try:
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user,
password=self.password, db=self.db, charset=self.charset, local_infile=self.local_infile)
self.cursor = self.conn.cursor()
self.diccursor = self.conn.cursor(pymysql.cursors.DictCursor)
except Exception, e:
logging.error('connect error', exc_info=True)
def dml(self, sql, val=None):
self.cursor.execute(sql, val)
def version(self):
self.cursor.execute('select version()')
return self.cursor.fetchone()
def dql(self, sql):
self.cursor.execute(sql)
return self.cursor.fetchall()
def commit(self):
self.conn.commit()
def close(self):
self.cursor.close()
self.diccursor.close()
self.conn.close()
db_host='127.0.0.1'
db_port=3308
db_user='fan'
db_passwd='fanboshi'
db_use='test'
from multiprocessing import Process
import pymysql
class LoadDataExtractor(Process):
def __init__(self,Fandb):
Process.__init__(self)
self.Fandb = Fandb
def run(self):
self.load_data()
def load_data(self):
conn1 = self.Fandb(db_host, db_port, db_user, db_passwd, db_use)
sql = "INSERT INTO new_mobile_ad ( DTIME, UNION_ID, AD_ID, IFA, MAC, CB_URL, STATE, DOMAIN, GAME_CODE ) VALUES ( %s, 14538, '', '865684037390353', '', '', 0, '520050', 'android' )"
conn1.dml(sql, (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
conn1.commit()
conn1.close()
while True:
processes = {}
for i in range(10):
processes[i] = LoadDataExtractor(Fandb)
processes[i].start()
time.sleep(5)