日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python异步框架twisted_Python学习八十七天:使用异步的twisted框架写入数据

發布時間:2025/3/8 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python异步框架twisted_Python学习八十七天:使用异步的twisted框架写入数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.twisted框架介紹

Twisted是用Python實現的基于事件驅動的網絡引擎框架;

Twisted支持許多常見的傳輸及應用層協議,包括TCP、UDP、SSL/TLS、HTTP、IMAP、SSH、IRC以及FTP。就像Python一樣,Twisted也具有“內置池”(batteries-included)的特點。Twisted對于其支持的所有協議都帶有客戶端和服務器實現,同時附帶有基于命令行的工具,使得配置和部署產品級的Twisted應用變得非常方便。

2.MySQL數據庫信息保存到settings文件中

首先我們需要把MySQL數據庫中的配置信息保存到settings文件中,如:MYSQL_HOST = 'localhost'的形式;

MYSQL_HOST = 'localhost'

MYSQL_USER = 'xkd'

MYSQL_PASSWORD = '123456'

MYSQL_DATABASE = 'item_database'

MYSQL_PORT = 3306

MYSQL_OPTIONAL = dict(

USE_UNICODE = True,

CHARSET = 'utf8',

)

然后從settings文件中將這些信息導入到pipeline.py文件中使用;

from .settings import MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_OPTIONAL

class MysqlPipeline:

def __init__(self):

self.conn = MySQLdb.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DATABASE, use_unicode=MYSQL_OPTIONAL.get('USE_UNICODE'), charset=MYSQL_OPTIONAL.get('CHARSET'))

self.cursor = self.conn.cursor()

def process_item(self, item, spider):

sql = 'insert into item(title, image_url, date, image_path, url, url_id)' \

'values (%s, %s, %s, %s, %s, %s)'

date = item['date']

self.cursor.execute(sql, args=(item['title'], item['image_url'], date, item['image_path'], item['url'], item['url_id']))

self.conn.commit()

return item

def spider_closed(self, spider):

self.cursor.close()

self.conn.close()

3.創建異步Pipeline寫入數據庫

首先創建一個用于異步寫入數據的AIOMysqlItemPipeline類,然后在這個類的初始化方法中創建一個pool連接池;

然后在from_settings()方法中獲取settings文件中的數據庫配置信息,并將配置信息存入一個字典中。使用Twisted中的adbapi獲取數據庫連接池對象,使用前需要導入adbapi,如:from twisted.enterprise import adbapi。使用時需要用到ConnectionPool連接池:pool=adbapi.ConnectionPool('MySQLdb',**params),參數MySQLdb是使用的數據庫引擎的名字,params就是要傳遞的數據庫配置信息;

接著在process_item()方法中使用數據庫連接池對象進行數據庫操作,自動傳遞cursor對象到數據庫操作方法runInteraction()的第一個參數(自定義方法)如:ret=self.connection_pool.runInteraction(self.mysql_insert,item);

還可以設置出錯時的回調方法,自動傳遞出錯消息對象failure到錯誤處理方法的第一個參數(自定義方法)如:ret.addErrback(self.error_callback);

最后記得修改settings文件中的ITEM_PIPELINES配置,如:'XKD_Dribbble_Spider.pipelines.AIOMysqlItemPipeline': 2;

from twisted.enterprise import adbapi

import MySQLdb.cursors

class AIOMysqlItemPipeline:

def __init__(self, pool):

self.connection_pool = pool

# 1:調用類方法

@classmethod

def from_settings(cls, settings):

connkw = {

'host': MYSQL_HOST,

'user': MYSQL_USER,

'password': MYSQL_PASSWORD,

'db': MYSQL_DATABASE,

'port': MYSQL_PORT,

'use_unicode': MYSQL_OPTIONAL.get('USE_UNICODE'),

'charset': MYSQL_OPTIONAL.get('CHARSET'),

'cursorclass': MySQLdb.cursors.DictCursor,

}

pool = adbapi.ConnectionPool('MySQLdb', **connkw)

return cls(pool)

# 2:執行process_item

def process_item(self, item, spider):

ret = self.connection_pool.runInteraction(self.mysql_insert, item)

ret.addErrback(self.error_callback)

def mysql_insert(self, cursor, item):

sql = 'insert into item(title, image_url, date, image_path, url, url_id)' \

'values (%s, %s, %s, %s, %s, %s)'

date = item['date']

cursor.execute(sql, args=(item['title'], item['image_url'], date, item['image_path'], item['url'], item['url_id']))

def error_callback(self, error):

print('insert_error =========== {}'.format(error))

修改settings文件

ITEM_PIPELINES = {

# 'XKD_Dribbble_Spider.pipelines.XkdDribbbleSpiderPipeline': 300,

# 當items.py模塊yield之后,默認就是下載image_url的頁面

'XKD_Dribbble_Spider.pipelines.ImagePipeline': 1,

'XKD_Dribbble_Spider.pipelines.AIOMysqlItemPipeline': 2,

}

總結

以上是生活随笔為你收集整理的python异步框架twisted_Python学习八十七天:使用异步的twisted框架写入数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。