在我的python脚本中,我订阅了一个Web套接字。每当收到数据时,我都会将此数据插入MySQL数据库。每秒大约有100-200个查询。问题是它工作了一段时间,然后它给出了错误"错误2006:MySQL服务器已经消失了#34; 我将Max_allowed_packets增加到512M。但它没有用。
这是我的代码。
def db_entry(threadName, _data):
_time = time.strftime('%Y-%m-%d %H:%M:%S')
#print ("starting new thread...")
for data in _data:
#print (data)
sql = "INSERT INTO %s (Script_Name, Lot_Size, Date, Time, Last_Price, Price_Change, Open,High, Low, Close, Volume, Buy_Quantity, Sell_Quantity) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" %("_" + str(data['instrument_token']), data['instrument_token'], 1, datetime.datetime.today().strftime("%Y-%m-%d"), _time, data['last_price'], data['change'], data['ohlc']['open'], data['ohlc']['high'], data['ohlc']['low'], data['ohlc']['close'], data['volume'], data['buy_quantity'], data['sell_quantity'])
cursor.execute(sql)
# Commit your changes in the database
db.commit()
def on_tick(tick, ws):
thread_name = "Thread" + str(thread_count + 1)
try:
_thread.start_new_thread(db_entry,(thread_name,tick, ))
except exception as e:
print (e)
raise
def on_connect(ws):
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe(instrument_token)
# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL,instrument_token)
# Assign the callbacks.
kws.on_tick = on_tick
kws.on_connect = on_connect
kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()
提前致谢。 :)
答案 0 :(得分:0)
MySQL开发人员文档提供的documentation在这一点上非常明确。可能的情况是,其中一些MySQL查询运行速度比其他查询慢,因为他们正在等待轮到他们插入数据。如果他们等待太久,MySQL将关闭他们的连接。默认情况下,MySQL的wait_timeout
为8小时(28800秒)。 MySQL配置是否经过调整?为MySQL分配了多少硬件?
通常,查看所有超时配置。阅读并理解它们。不要简单地复制和粘贴所有性能调整博客,如博客。
答案 1 :(得分:0)
最后,它已经解决了。 我保持数据库连接打开,这导致了问题。 我在触发查询时关闭数据库连接。当想再次插入东西时再次打开。
答案 2 :(得分:0)
您需要使用自己的连接处理方法创建对象。我用它并运作良好。
class DB():
def __init__(self, **kwargs):
self.conn = MySQLdb.connect(‘host’, ‘user’, ‘pass’, ‘db’)
try:
if (self.conn):
status = "DB init success"
else:
status = "DB init failed"
self.conn.autocommit(True)
# self.conn.select_db(DB_NAME)
self.cursor = self.conn.cursor()
except Exception as e:
status = "DB init fail %s " % str(e)
def execute(self, query):
try:
if self.conn is None:
self.__init__()
else:
self.conn.ping(True)
self.cursor.execute(query)
return self.cursor.fetchall()
except Exception as e:
import traceback
traceback.print_exc()
# error ocurs,rollback
self.conn.rollback()
return False
用法
data = DB().execute("SELECT * FROM Users")
print(data)