python如何通過(guò)twisted實(shí)現(xiàn)數(shù)據(jù)庫(kù)異步插入
如何通過(guò)twisted實(shí)現(xiàn)數(shù)據(jù)庫(kù)異步插入?
1. 導(dǎo)入adbapi
2. 生成數(shù)據(jù)庫(kù)連接池
3. 執(zhí)行數(shù)據(jù)數(shù)據(jù)庫(kù)插入操作
4. 打印錯(cuò)誤信息,并排錯(cuò)
#!/usr/bin/python3
__author__ = 'beimenchuixue'
__blog__ = 'http://www.cnblogs.com/2bjiujiu/'
import pymysql
from twisted.enterprise import adbapi
from twisted.internet import reactor
def go_insert(cursor, sql):
# 對(duì)數(shù)據(jù)庫(kù)進(jìn)行插入操作,并不需要commit,twisted會(huì)自動(dòng)幫我commit
try:
for i in range(10):
data = str(i)
cursor.execute(sql, data)
except Exception as e:
print(e)
def handle_error(failure):
# 打印錯(cuò)誤
if failure:
print(failure)
if __name__ == '__main__':
# 數(shù)據(jù)庫(kù)基本配置
db_settings = {
'host': 'localhost',
'db': 'jobole',
'user': 'root',
'password': 'passwort',
'charset': 'utf8',
'use_unicode': True
}
# sql語(yǔ)句模版
insert_sql = 'insert into test_1(text_1) value(%s)'
# 普通方法插入數(shù)據(jù)
# conn = pymysql.connect(**db_settings)
# cursor = conn.cursor()
# cursor.execute(insert_sql, '1')
# conn.commit()
try:
# 生成連接池
db_conn = adbapi.ConnectionPool('pymysql', **db_settings)
# 通過(guò)連接池執(zhí)行具體的sql操作,返回一個(gè)對(duì)象
query = db_conn.runInteraction(go_insert, insert_sql)
# 對(duì)錯(cuò)誤信息進(jìn)行提示處理
query.addCallbacks(handle_error)
except Exception as e:
print(e)
# 定時(shí),給4秒時(shí)間讓twisted異步框架完成數(shù)據(jù)庫(kù)插入異步操作,沒(méi)有定時(shí)什么都不會(huì)做
reactor.callLater(4, reactor.stop)
reactor.run()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python如何通過(guò)twisted搭建socket服務(wù)
- Python3.6中Twisted模塊安裝的問(wèn)題與解決
- python安裝twisted的問(wèn)題解析
- python基于twisted框架編寫(xiě)簡(jiǎn)單聊天室
- python 編程之twisted詳解及簡(jiǎn)單實(shí)例
- Python 基于Twisted框架的文件夾網(wǎng)絡(luò)傳輸源碼
- 剖析Python的Twisted框架的核心特性
- 實(shí)例解析Python的Twisted框架中Deferred對(duì)象的用法
- 詳解Python的Twisted框架中reactor事件管理器的用法
- 使用Python的Twisted框架編寫(xiě)非阻塞程序的代碼示例
- Python的Twisted框架中使用Deferred對(duì)象來(lái)管理回調(diào)函數(shù)
- 使用Python的Twisted框架構(gòu)建非阻塞下載程序的實(shí)例教程
- Python的Twisted框架上手前所必須了解的異步編程思想
- 使用Python的Treq on Twisted來(lái)進(jìn)行HTTP壓力測(cè)試
- 利用Python的Twisted框架實(shí)現(xiàn)webshell密碼掃描器的教程
- 使用Python的Twisted框架實(shí)現(xiàn)一個(gè)簡(jiǎn)單的服務(wù)器
- 使用Python的Twisted框架編寫(xiě)簡(jiǎn)單的網(wǎng)絡(luò)客戶(hù)端
- python開(kāi)發(fā)實(shí)例之Python的Twisted框架中Deferred對(duì)象的詳細(xì)用法與實(shí)例
相關(guān)文章
python實(shí)現(xiàn)簡(jiǎn)易學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Python網(wǎng)絡(luò)請(qǐng)求庫(kù)requests的10個(gè)基本用法
今天我們要聊聊Python中非常實(shí)用的一個(gè)庫(kù)——requests,這個(gè)庫(kù)讓發(fā)送HTTP請(qǐng)求變得超級(jí)簡(jiǎn)單,無(wú)論你是想抓取網(wǎng)頁(yè)數(shù)據(jù)還是測(cè)試API接口,requests都能派上大用場(chǎng),下面我們就一起來(lái)看看如何使用requests完成一些常見(jiàn)的任務(wù),需要的朋友可以參考下2024-10-10
使用python實(shí)現(xiàn)生成用戶(hù)信息
這篇文章主要介紹了使用python實(shí)現(xiàn)生成用戶(hù)信息的相關(guān)代碼,非常的簡(jiǎn)單實(shí)用,需要的朋友可以參考下2017-03-03
Python添加時(shí)間軸以實(shí)現(xiàn)動(dòng)態(tài)繪圖詳解
這篇文章主要為大家詳細(xì)介紹了Python如何添加時(shí)間軸以實(shí)現(xiàn)動(dòng)態(tài)繪圖,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以參考一下2023-09-09
Python實(shí)現(xiàn)快速傅里葉變換的方法(FFT)
這篇文章主要介紹了Python實(shí)現(xiàn)快速傅里葉變換的方法(FFT),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
python 串口讀取+存儲(chǔ)+輸出處理實(shí)例
今天小編就為大家分享一篇python 串口讀取+存儲(chǔ)+輸出處理實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12

