私のpythonスクリプトでは、私はWebソケットを購読しました。データが受信されるたびに、私はこのデータをMySQLデータベースに挿入します。毎秒約100-200のクエリがあります。問題はそれがしばらくの間働いていて、 "エラー2006:MySQLサーバーがなくなった"というエラーを返します。 Max_allowed_packetsを512Mまで増やしました。しかしそれはうまくいかなかった。MySQLサーバはpythonを廃止MySQLdb
ここに私のコードです。
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()
ありがとうございます。 :)
答えていただきありがとうございます。残念ながら、エラーを引き起こす何かがありましたが、私はMySQLが提供するドキュメントを一度も読んでいません。だから私はそれとしばらく時間を費やすつもりです。再度お返事ありがとうございます! –