問題
mysql 查詢出現錯誤
error: (2014, "Commands out of sync; you can't run this command now")1
查詢
mysql文檔中的解釋
If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.
This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.
調用順序錯誤,同一個連接,發出2個查詢請求,第一個請求發出之后沒有等到mysql返回就發出第二個請求
背景 思考
我這里的程序是這樣的,在django框架中起了一個定時任務,這個任務中有個循環,主線程循環查詢mysql然后在循環體中生成了子進程,子進程中也有mysql查詢。
我測試了下不實用多進程的情況沒有問題,使用多進程就會出現這個問題。
對照上面的文檔,其實不難想到,錯誤應該是這樣的
解決
解決的方案其實很容易想到,就是當我們fork一個進程之后,讓他從新獲取一個和mysql的連接C或者D就好了嘛,
結果幾個測試,得到如下的方案。
在父進程的loop中,創建子進程之前關閉mysql連接,這樣子進程中就會重新連接mysql。
from django import db db.close_connection() p = Process(target=ap5mintes_scan, args=(ac, details, mtime)) p.start()
其實就是狀態copy的問題,本來多個線程同時并發調用一個connection也不對.
后面做了個測試 ,多進程的情況下查看mysql processlist,的確使用建立多個mysql 連接。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
|
新聞熱點
疑難解答