<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        Python中MySQLdb和torndb模塊對MySQL的斷連問題處理

        來源:懂視網 責編:小采 時間:2020-11-27 14:41:57
        文檔

        Python中MySQLdb和torndb模塊對MySQL的斷連問題處理

        Python中MySQLdb和torndb模塊對MySQL的斷連問題處理:在使用python 對wordpress tag 進行細化代碼處理時,遇到了調用MySQLdb模塊時的出錯,由于錯誤提示和問題原因相差甚遠,查看了N久代碼也未發現代碼有問題。后來問了下師傅,被告知MySQLdb里有一個斷接的坑 ,需要進行數據庫重連解決。 一、報錯代碼及提示
        推薦度:
        導讀Python中MySQLdb和torndb模塊對MySQL的斷連問題處理:在使用python 對wordpress tag 進行細化代碼處理時,遇到了調用MySQLdb模塊時的出錯,由于錯誤提示和問題原因相差甚遠,查看了N久代碼也未發現代碼有問題。后來問了下師傅,被告知MySQLdb里有一個斷接的坑 ,需要進行數據庫重連解決。 一、報錯代碼及提示

        在使用python 對wordpress tag 進行細化代碼處理時,遇到了調用MySQLdb模塊時的出錯,由于錯誤提示和問題原因相差甚遠,查看了N久代碼也未發現代碼有問題。后來問了下師傅,被告知MySQLdb里有一個斷接的坑 ,需要進行數據庫重連解決。

        一、報錯代碼及提示

        運行出錯的代碼如下:

        import MySQLdb
        def getTerm(db,tag):
         cursor = db.cursor()
         query = "SELECT term_id FROM wp_terms where name=%s "
         count = cursor.execute(query,tag)
         rows = cursor.fetchall()
         db.commit()
         #db.close()
         if count:
         term_id = [int(rows[id][0]) for id in range(count)]
         return term_id
         else:return None
        def addTerm(db,tag):
         cursor = db.cursor()
         query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
         data = (tag,tag)
         cursor.execute(query,data)
         db.commit()
         term_id = cursor.lastrowid
         sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
         value = (term_id,tag)
         cursor.execute(sql,value)
         db.commit()
         db.close()
         return int(term_id)
        dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
        tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
        tagids = []
        for tag in tags:
         termid = getTerm(dbconn,tag)
         if termid:
         print tag, 'tag id is ',termid
         tagids.extend(termid)
         else:
         termid = addTerm(dbconn,tag)
         print 'add tag',tag,'id is ' ,termid
         tagids.append(termid)
        print 'tag id is ',tagids
        

        直接可以執行,在第for循環里第二次調用getTerm函數時,報錯如下:

        Traceback (most recent call last):
         File "a.py", line 40, in 
         termid = getTerm(dbconn,tag)
         File "a.py", line 11, in getTerm
         count = cursor.execute(query,tag)
         File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 154, in execute
         charset = db.character_set_name()
        _mysql_exceptions.InterfaceError: (0, '')
        
        

        二、解決方法

        初始時以為是編碼問題了,又細核對了幾遍未發現編碼有問題,在python代碼里也未發現異常。后來問過師傅后,師傅來了句提示:

        只看代碼有啥用,mysql 的超時時間調長點或捕獲異常從連,原因是
        cursor. connection 沒有關閉
        但是socket已經斷了
        cursor 這個行為不會再建立一次socket的
        重新執行一次MysqlDB.connect()
        看的有點懵懂,先從mysql 里查看了所有timeout相關的變量

        mysql> show GLOBAL VARIABLES like "%timeout%";
        

        +----------------------------+-------+
        | Variable_name | Value |
        +----------------------------+-------+
        | connect_timeout | 10 |
        | delayed_insert_timeout | 300 |
        | innodb_lock_wait_timeout | 50 |
        | innodb_rollback_on_timeout | OFF |
        | interactive_timeout | 28800 |
        | net_read_timeout | 30 |
        | net_write_timeout | 60 |
        | slave_net_timeout | 3600 |
        | table_lock_wait_timeout | 50 |
        | wait_timeout | 28800 |
        +----------------------------+-------+
        10 rows in set (0.00 sec)
        

        發現最小的超時時間是10s ,而我的程序執行起來顯然就不了10s 。因為之前查過相關的報錯,這里估計這個很可能是另外一個報錯:2006,MySQL server has gone away 。即然和這個超時時間應該沒關系,那就嘗試通過MySQLdb ping測試,如果捕獲異常,就再進行重連,修改后的代碼為:

        #!/usr/bin/python
        #coding=utf-8
        import MySQLdb
        def getTerm(db,tag):
         cursor = db.cursor()
         query = "SELECT term_id FROM wp_terms where name=%s "
         count = cursor.execute(query,tag)
         rows = cursor.fetchall()
         db.commit()
         #db.close()
         if count:
         term_id = [int(rows[id][0]) for id in range(count)]
         print term_id
         return term_id
         else:return None
        def addTerm(db,tag):
         cursor = db.cursor()
         query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
         data = (tag,tag)
         cursor.execute(query,data)
         db.commit()
         term_id = cursor.lastrowid
         sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
         value = (term_id,tag)
         cursor.execute(sql,value)
         db.commit()
         db.close()
         return int(term_id)
        dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
        tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
        if __name__ == "__main__":
         tagids = []
         for tag in tags:
         try:
         dbconn.ping()
         except:
         print 'mysql connect have been close'
         dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
         termid = getTerm(dbconn,tag)
         if termid:
         print tag, 'tag id is ',termid
         tagids.extend(termid)
         else:
         termid = addTerm(dbconn,tag)
         print 'add tag',tag,'id is ' ,termid
         tagids.append(termid)
         print 'All tags id is ',tagids
        

        再執行發現竟然OK了,而細看下結果,發現基本上每1-2次getTerm或addTerm函數調用就會打印一次'mysql connect have been close' 。

        三、使用torndb模塊解決mysql斷連問題
        1.MySQLdb和torndb的代碼樣例對比
        torndb是facebook開源的一個基于MySQLdb二次封裝的一個mysql模塊,新封裝的這個模塊比較小,是一個只有2百多行代碼的py文件。雖然代碼短,功能確相較MySQLdb簡便不少,并且該模塊由于增加了reconnect方法和max_idel_time參數,解決了mysql的斷連問題。比較下使用原生MySQLdb模塊和使用torndb模塊的代碼:
        使用MySQLdb模塊的代碼

        import MySQLdb
        def getTerm(db,tag):
         cursor = db.cursor()
         query = "SELECT term_id FROM wp_terms where name=%s "
         count = cursor.execute(query,tag)
         rows = cursor.fetchall()
         db.commit()
         #db.close()
         if count:
         term_id = [int(rows[id][0]) for id in range(count)]
         return term_id
         else:return None
        def addTerm(db,tag):
         cursor = db.cursor()
         query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
         data = (tag,tag)
         cursor.execute(query,data)
         db.commit()
         term_id = cursor.lastrowid
         sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
         value = (term_id,tag)
         cursor.execute(sql,value)
         db.commit()
         db.close()
         return int(term_id)
        def addCTag(db,data):
         cursor = db.cursor()
         query = '''INSERT INTO `wp_term_relationships` (
         `object_id` ,
         `term_taxonomy_id`
         )
         VALUES (
         %s, %s) '''
         cursor.executemany(query,data)
         db.commit()
         db.close()
        dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
        tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
        tagids = []
        for tag in tags:
         if termid:
         try:
         dbconn.ping()
         except:
         dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
         print tag, 'tag id is ',termid
         termid = getTerm(dbconn,tag)
         tagids.extend(termid)
         else:
         try:
         dbconn.ping()
         except:
         dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
         termid = addTerm(dbconn,tag)
         print 'add tag',tag,'id is ' ,termid
         tagids.append(termid)
        print 'tag id is ',tagids
        postid = '35'
        tagids = list(set(tagids))
        ctagdata = []
        for tagid in tagids:
         ctagdata.append((postid,tagid))
        try:
         dbconn.ping()
        except:
         dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
         addCTag(dbconn,ctagdata)
        

        使用torndb的代碼

        #!/usr/bin/python
        #coding=utf-8
        import torndb
        def getTerm(db,tag):
         query = "SELECT term_id FROM wp_terms where name=%s "
         rows = db.query(query,tag)
         termid = []
         for row in rows:
         termid.extend(row.values())
         return termid
        def addTerm(db,tag):
         query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
         term_id = db.execute_lastrowid(query,tag,tag)
         sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
         db.execute(sql,term_id,tag)
         return term_id
        def addCTag(db,data):
         query = "INSERT INTO wp_term_relationships (object_id,term_taxonomy_id) VALUES (%s, %s) "
         db.executemany(query,data)
        dbconn = torndb.Connection('localhost:3306','361way',user='root',password='123456')
        tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
        tagids = []
        for tag in tags:
         termid = getTerm(dbconn,tag)
         if termid:
         print tag, 'tag id is ',termid
         tagids.extend(termid)
         else:
         termid = addTerm(dbconn,tag)
         print 'add tag',tag,'id is ' ,termid
         tagids.append(termid)
        print 'All tags id is ',tagids
        postid = '35'
        tagids = list(set(tagids))
        ctagdata = []
        for tagid in tagids:
         ctagdata.append((postid,tagid))
        addCTag(dbconn,ctagdata)
        

        從兩者的代碼上來看,使用torndb模塊和原生相比,發現可以省略如下兩部分:

        torndb模塊不需要db.cursor進行處理,無不需要db.comment提交,torndb是自動提交的;

        torndb不需要在每次調用時,進行db.ping()判斷數據庫socket連接是否斷開,因為torndb增加了reconnect方法,支持自動重連。

        2.torndb的方法

        torndb提供的參數和方法有:

        execute 執行語句不需要返回值的操作。
        execute_lastrowid 執行后獲得表id,一般用于插入后獲取返回值。
        executemany 可以執行批量插入。返回值為第一次請求的表id。
        executemany_rowcount 批量執行。返回值為第一次請求的表id。
        get 執行后獲取一行數據,返回dict。
        iter 執行查詢后,返回迭代的字段和數據。
        query 執行后獲取多行數據,返回是List。
        close 關閉
        max_idle_time 最大連接時間
        reconnect 關閉后再連接
        使用示例:

        mysql> CREATE TABLE `ceshi` (`id` int(1) NULL AUTO_INCREMENT ,`num` int(1) NULL ,PRIMARY KEY (`id`));
        

        >>> import torndb
        >>> db = torndb.Connection("127.0.0.1","數據庫名","用戶名", "密碼", 24*3600) # 24*3600為超時時間
        >>> get_id1 = db.execute_lastrowid("insert ceshi(num) values('1')")
        >>> print get_id1
        1
        >>> args1 = [('2'),('3'),('4')]
        >>> get1 = db.executemany("insert ceshi(num) values(%s)", args1)
        >>> print get1
        2
        >>> rows = db.iter("select * from ceshi")
        >>> for i in rows:
        … print i
        

        3.報錯

        在使用過程中可能遇到的錯誤:

         File "/home/361way/database.py", line 145, in execute_lastrowid
         self._execute(cursor, query, parameters)
         File "/home/361way/database.py", line 207, in _execute
         return cursor.execute(query, parameters)
         File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 159, in execute
         query = query % db.literal(args)
        TypeError: not enough arguments for format string
        

        寫上面的代碼時,我剛開始還是試著使用MySQLdb模塊的方式引用數據,結果發現報參數的錯誤 ,經查看代碼發現 ,torndb在使用幾個sql方法時較MySQLdb精簡過了。具體各個方法的傳參方法如下(注意參數個數):

        close()
        reconnect()
        iter(query, *parameters, **kwparameters)
        query(query, *parameters, **kwparameters)
        get(query, *parameters, **kwparameters)
        execute(query, *parameters, **kwparameters)
        execute_lastrowid(query, *parameters, **kwparameters)
        execute_rowcount(query, *parameters, **kwparameters)
        executemany(query, parameters)
        executemany_lastrowid(query, parameters)
        executemany_rowcount(query, parameters)
        update(query, *parameters, **kwparameters)
        updatemany(query, parameters)
        insert(query, *parameters, **kwparameters)
        insertmany(query, parameters)
        

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Python中MySQLdb和torndb模塊對MySQL的斷連問題處理

        Python中MySQLdb和torndb模塊對MySQL的斷連問題處理:在使用python 對wordpress tag 進行細化代碼處理時,遇到了調用MySQLdb模塊時的出錯,由于錯誤提示和問題原因相差甚遠,查看了N久代碼也未發現代碼有問題。后來問了下師傅,被告知MySQLdb里有一個斷接的坑 ,需要進行數據庫重連解決。 一、報錯代碼及提示
        推薦度:
        標簽: mysql python pyth
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲午夜理论片在线观看| 亚洲精品午夜无码专区| 亚洲国产夜色在线观看| 在线看片免费人成视频播| 亚洲最大福利视频| 亚洲黄色免费在线观看| 亚洲精彩视频在线观看| 亚洲日本在线免费观看| 亚洲精品国产免费| 插B内射18免费视频| 亚洲av片在线观看| 亚洲精品无码av天堂| 久久久久久久久久久免费精品| 国产亚洲大尺度无码无码专线| 亚洲免费人成在线视频观看 | 四虎影视在线影院在线观看免费视频| 亚洲精品中文字幕乱码三区| 美丽姑娘免费观看在线观看中文版 | jizz免费一区二区三区| 亚洲欧洲无码AV电影在线观看 | 女人18毛片水真多免费看| 小说专区亚洲春色校园| 国产亚洲精品看片在线观看| 亚洲电影免费在线观看| 亚洲欧洲日产韩国在线| 最新中文字幕免费视频| 免费国产高清毛不卡片基地| 精品亚洲综合在线第一区| 四虎在线视频免费观看视频| 亚洲日本成本人观看| 毛茸茸bbw亚洲人| 四虎免费影院ww4164h| 美女视频黄频a免费观看| 亚洲av永久无码精品秋霞电影影院| 久久精品亚洲日本波多野结衣 | 91精品国产亚洲爽啪在线影院| 一区二区三区在线免费| 久久亚洲sm情趣捆绑调教| 99久久免费精品国产72精品九九| 有码人妻在线免费看片| 亚洲午夜电影在线观看|