#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-04-25 18:04:06 # @Author : Your Name ([email protected]) # @Link : http://example.org # @Version : $Id$ import threading,time,MySQLdb class TestMysql(): def __init__(self, ip, port, username, password, database): self.db = MySQLdb.connect(host=ip, port=port, user=username, passwd=password, db=database, charset="utf8") self.cursor = self.db.cursor() def select1(self,tablename): sql = "SELECT (gender & 15) AS gender1, f.image_data,f.json FROM intellif_face." + tablename + " f limit 100000;" self.cursor.execute(sql) data = self.cursor.fetchone() print(data) def select2(self,tablename): sql = "SELECT (race & 15) AS race1,f.image_data,f.json FROM intellif_face." + tablename + " f limit 100000;" self.cursor.execute(sql) data = self.cursor.fetchone() print(data) if __name__ == '__main__': test = TestMysql("192.168.11.128", 3306, "root", 'introcks1234', "intellif_face") tablelist = ['t_face_24','t_face_25','t_face_26'] # 可以增加表名 thread = [] for tablename in tablelist: print(tablename) t1 = threading.Thread(target = test.select1, args = (tablename,)) t2 = threading.Thread(target = test.select2, args = (tablename,)) thread.append(t1) thread.append(t2) for t in thread: t.setDaemon(True) t.start() t.join() ============================output=========================================
Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python36\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Python36\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "F:\code\pyProject\thread\thread_mysql_test.py", line 25, in select2 self.cursor.execute(sql) File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 250, in execute self.errorhandler(self, exc, value) File "C:\Python36\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler raise errorvalue File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 247, in execute res = self._query(query) File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 411, in _query rowcount = self._do_query(q) File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query db.query(q) File "C:\Python36\lib\site-packages\MySQLdb\connections.py", line 277, in query _mysql.connection.query(self, query) _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query') ....... [Finished in 1.9s with exit code 3221226356] 