Scrapy基礎(十二)————非同步匯出Item資料到Mysql中

來源:互聯網
上載者:User

標籤:enter   mysql   rbac   unicode   編碼   com   建立對象   date   turn   

  非同步匯出資料到Mysql中

  上次說過從Item中同步寫入資料庫,因為網路的下載速度和資料庫的I/O速度是不一樣的
所以有可能會發生下載快,但是寫入資料庫速度慢,造成線程的堵塞;關於堵塞和非堵塞,同步和非同步
的關係,因為沒接觸過,所以也不是很好的理解,這裡查看了些資料,已做記錄
連結1:https://www.zhihu.com/question/19732473;http://wiki.jikexueyuan.com/project/twisted-intro/

 

實現非同步寫入mysql資料庫的思路:    

    1,將資料庫的串連資料寫入到settings檔案中,供後面自訂的pipeline使用
    2,自訂pipeline,使用Twisted架構實現非同步
    3,在settings中註冊這個管道
具體代碼實現:

1:settings中設定;前提是資料庫已經設計好了,參考同步機制寫入Mysql中

 

2:
還有匯入import MySQLdb.cursors

 1 from twisted.enterprise import adbapi 2 class MysqlTwistedPipeline(object): 3     ‘‘‘ 4     非同步機制將資料寫入到mysql資料庫中 5     ‘‘‘ 6     #建立初始化函數,當通過此類建立對象時首先被調用的方法 7     def __init__(self,dbpool): 8         self.dbpool = dbpool 9     #建立一個靜態方法,靜態方法的載入記憶體優先順序高於init方法,java的static方法類似,10     #在建立這個類的對之前就已將載入到了記憶體中,所以init這個方法可以調用這個方法產生的對象11     @classmethod12     #名稱固定的13     def from_settings(cls,settings):14         #先將setting中串連資料庫所需內容取出,構造一個地點15         dbparms = dict(16             host= settings["MYSQL_HOST"],17             db = settings["MYSQL_DBNAME"],18             user = settings["MYSQL_USER"],19             passwd = settings["MYSQL_PASSWORD"],20             charset = "utf-8",21             #遊標設定22             cursorclass = Mysqldb.cursors.DictCursor,    23             #設定編碼是否使用Unicode24             use_unicode = True25         )26         #通過Twisted架構提供的容器串連資料庫,MySQLdb是資料庫模組名27         dbpool = adbapi.ConnectionPool("MySQLdb",dbparms)28         return cls(dbpool)29 30     def process_item(self,item,spider):31         #使用Twisted非同步將Item資料插入資料庫32         query = self.dbpool.runInteraction(self.do_insert,item)33         query.addErrback(self.handle_error,item,spider)#這裡不往下傳入item,spider,handle_error則不需接受,item,spider)34     35     def do_insert(self,cursor,item):36         #執行具體的插入語句,不需要commit操作,Twisted會自動進行37         insert_sql = """38              insert into jobbole_artical(title,creat_date,url,url_object_id,39                  front_image_url2,front_image_path,tags,comment_num,40                  fav_num,like_num,content41                  )42              VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)43         """44         cursor.execute(insert_sql,(item["title"],item["creat_date"],item["url"],45             item["url_object_id"],item["front_image_url2"],item["front_image_path"],46             item["tags"],item["comment_num"],item["fav_num"],item["like_num"],item["content"]))47 48     def handle_error(self, failure, item, spider):49         #出來非同步插入異常50         print(failure)

3:註冊settings中:

 

至此,非同步將Item的資料寫入Mysql就完成了;
展示結果:不知道為什麼,感覺非同步寫入比同步寫入稍微慢點(相同電腦和網速條件下),還請明白的大神指點下


 

Scrapy基礎(十二)————非同步匯出Item資料到Mysql中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.