#!/usr/bin/python
from __future__ import division
from socket import gethostname;
import threading
import sys
import os
import MySQLdb
class threader(threading.Thread):
def __init__(self,method):
threading.Thread.__init__(self)
self.tx =
self.method = method
def run(self):
run_insert()
def run_insert():
sql = "INSERT INTO table (`id`,`A`,`B`,`C`) VALUES (NULL,'0','0','0');")
try:
cursor.execute(sql)
db.commit()
except:
print "insert failed"
def init_thread(): backgrounds = []
for db in connections:
logger("Spawning thread: %s"%(db),"d")
quant = tx / THREADS
background = threader(method,quant,db)
background.start()
backgrounds.append(background)
for background in backgrounds:
background.join()
def main():
try:
init_thread()
except:
print "failed to initiate threads"
sys.exit(0)
if __name__ == "__main__":
mysql_host = "localhost" #default localhost
mysql_pass = "pass" #default dbbench
mysql_user = "user" #default dbbench
mysql_port = 3306 #default 3306
mysql_db = "schema" #default dbbench
threads = 4 #must be INT not STR #create connection pool
connections = []
for thread in range(THREADS):
try:
connections.append(MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_pass, db=mysql_db, port=mysql_port))
except MySQLdb.Error, e:
print "Error %d: %s"%(e.args[0], e.args[1])
sys.exit (1)
main()
python 多線程插入mysql
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
MySQL数据库集群-PXC方案
wy53780
2020-04-23 13:55:07
sql語法、關係建模
死瘦宅
2019-02-24 22:35:22
MySQL 事務、函數、觸發器、數據庫備份、權限管理
死瘦宅
2019-02-24 22:35:12
《從0到1學習Flink》—— Flink 讀取 Kafka 數據批量寫入到 MySQL
zhisheng
2019-02-24 21:04:11
SpringBoot 填坑 (一) | CentOS7.4 環境下,表時間字段默認值設置失效
一個優秀的廢人
2019-02-24 15:33:55
mysql-存儲過程
雙子城
2019-02-24 13:43:57
Python3高级核心技术97讲
wy53780
2020-04-23 15:37:39
python網絡爬蟲
aabcdqt
2019-05-06 20:35:05
Python與家國天下
豌豆花下貓
2019-02-24 22:22:40
seaborn 安裝成功 + ImportError: DLL load failed: 找不到指定的模塊 問題解決
耗子
2019-02-24 19:55:04
使用 Sphinx 撰寫技術文檔並生成 PDF 總結
qiwihui
2019-02-24 16:25:14
Python實現二叉樹相關算法
backbp
2019-02-24 15:35:06
Ubuntu16.04安裝Python3.7及其pip3並切換爲默認版本
ipromise
2019-02-24 14:15:00
Python2+Selenium入門05-關於WebElement類
白雪
2019-02-24 13:45:04
Python——數據類型
陳晨工程師
2019-02-24 13:43:55