Python的Tornado框架實(shí)現(xiàn)異步非阻塞訪問數(shù)據(jù)庫(kù)的示例
tornado即是一個(gè)http非阻塞服務(wù)器, 就要用起來, 我們將用到tornado框架 ,mongodb數(shù)據(jù)庫(kù) 以及motor(mongodb的異步驅(qū)動(dòng)).來簡(jiǎn)單實(shí)現(xiàn)tornado的非阻塞功能.
其他環(huán)境支持的下載與安裝
1.安裝mongodb
$ sudo apt-get install update $ sudo apt-get install mongodb
2.安裝motor
$ pip install motor
非阻塞
# conf.py
import os
import motor
from handlers import index, auth
BASE_DIR = os.path.join(__file__)
handlers = [
(r'^/$', index.IndexHandler),
(r'^/auth/register$', auth.RegisterHandler),
(r'^/auth/login$', auth.LoginHandler),
]
settings = dict(
debug = True,
template_path = os.path.join(BASE_DIR, 'templates'),
static_path = os.path.join(BASE_DIR, 'static'),
)
client = motor.MotorClient("127.0.0.1")
db = client.meet
首先在配置文件中連接數(shù)據(jù)庫(kù), client.db_name中 db_name就是數(shù)據(jù)庫(kù)的名稱
# handlers/__init__.py
class BaseHandler(tornado.web.RequestHandler, TemplateRendering):
def initialite(self):
...
@property
def db(self):
return self.application.db
添加db()并使用property裝飾,像屬性一樣訪問數(shù)據(jù)庫(kù).
# auth.py
import os
import time
import tornado.web
from tornado import gen
from . import BaseHandler
class RegisterHandler(BaseHandler):
def get(self):
self.render_html('register.html')
@tornado.web.asynchronous
@gen.coroutine
def post(self):
username = self.get_argument('username', None)
email = self.get_argument('email', None)
password = self.get_argument('password', None)
data = {
'username': username,
'email': email,
'password': password,
'timestamp': time.time() * 1000,
}
if username and email:
yield self.db.user.insert(data)
self.redirect('/')
class LoginHandler(BaseHandler):
@tornado.web.asynchronous
@gen.coroutine
def get(self):
username = self.get_argument('useranme')
user = yield self.db.user.find_one({'username': username})
self.render_html('login.html', user=user)
@gen.coroutine裝飾使函數(shù)非阻塞, 返回一個(gè)生成器, 而不用在使用回調(diào)函數(shù). motor也通過yield 實(shí)現(xiàn)異步(不然還得返回一個(gè)回調(diào)函數(shù)). 其實(shí)這個(gè)例子反映不了阻塞問題關(guān)鍵是時(shí)間太短.
我們修改一下代碼
# 之前 yield self.db.user.insert(data) # 之后 yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 10)
這里通過tornado.ioloop.IOLoop.instance().add_timeout阻塞應(yīng)用, 這是time.sleep的非阻塞實(shí)現(xiàn), 如果這里使用time.sleep因?yàn)槭莟ornado是單線程會(huì)阻塞整個(gè)應(yīng)用所以別的handler也無法訪問.
可以看到我在注冊(cè)頁(yè)面注冊(cè)后,在阻塞期間點(diǎn)擊/auth/login直接就訪問了login頁(yè)完成非阻塞.
異步下的redirect問題
在使用tornado的時(shí)候常常遇到一些問題, 特將遇到的問題和解決的方法寫出來(這里的感謝一下幫我解答疑惑的pythonista們)
1.問題
我想要實(shí)現(xiàn)一個(gè)注冊(cè)用戶功能, web框架使用tornado數(shù)據(jù)庫(kù)使用mongodb但在注冊(cè)時(shí)出現(xiàn)Exception redirect的錯(cuò)誤. 現(xiàn)貼下代碼:
class Register(BaseHandler):
def get(self):
self.render_html('register.html')
@tornado.web.aynchronous
@gen.coroutine
def post(self):
username = self.get_argument('username')
email = self.get_argument('email')
password = self.get_argument('password')
captcha = self.get_argument('captcha')
_verify_username = yield self.db.user.find_one({'username': username})
if _verify_username:
self.flash(u'用戶名已存在', 'error')
self.redirect('/auth/register')
_verify_email = yield self.db.user.find_one({'email': email})
if _verify_email:
self.flash(u'郵箱已注冊(cè)', 'error')
self.redirect('/auth/register')
if captcha and captcha == self.get_secure_cookie('captcha').replace(' ',''):
self.flash(u'驗(yàn)證碼輸入正確', 'info')
else:
self.flash(u'驗(yàn)證碼輸入錯(cuò)誤', 'error')
self.redirect('/auth/register')
password = haslib.md5(password + self.settings['site']).hexdigest()
profile = {'headimg': '', 'site': '', 'job': '', 'signature':'',
'github': '', 'description': ''}
user_profile = yield self.db.profile.insert(profile)
user = {'username': username, 'email': email, 'password': password,
'timestamp': time.time(), 'profile_id': str(user_profile)}
yield self.db.user.insert(user)
self.set_secure_cookie('user', username)
self.redirect('/')
本想如果用戶驗(yàn)證碼輸入出錯(cuò)就跳轉(zhuǎn)到注冊(cè)頁(yè)面, 但問題是驗(yàn)證碼出錯(cuò)也會(huì)繼續(xù)執(zhí)行一下代碼. 雖然在self.redirect后加上self.finish會(huì)終止代碼,但是因?yàn)閟elf.redirect 函數(shù)內(nèi)已有self.finish所以出現(xiàn)了兩次報(bào)出異常終止的代碼.
因?yàn)橐陨显虼a不會(huì)被終結(jié), 驗(yàn)證碼出錯(cuò)用戶還是會(huì)注冊(cè).
2.解決方案
return self.redirect('/auth/register')
或
self.redirect('/auth/register')
return
(1)segmentdefault中熱心用戶rsj217給出的答案
self.finish 會(huì)關(guān)掉請(qǐng)求, 因?yàn)锧tornado.web.aynchronous告訴tornado會(huì)一直等待請(qǐng)求(長(zhǎng)鏈接). self.redirect等于設(shè)置了response的headers的location屬性.
(2)segmentdefault中熱心用戶依云給出的答案
self.finish當(dāng)然不會(huì)跳出函數(shù), 不然請(qǐng)求結(jié)束之后還想做些事情怎么辦呢.
3.總結(jié)
因?yàn)殄e(cuò)把self.finish當(dāng)做跳出函數(shù)出現(xiàn)了以上的問題
- self.redirect會(huì)在request.headers 里設(shè)置location用于跳轉(zhuǎn)
- self.finish會(huì)關(guān)掉請(qǐng)求, 但不會(huì)跳出函數(shù)
相關(guān)文章
python?實(shí)現(xiàn)?pymysql?數(shù)據(jù)庫(kù)操作方法
這篇文章主要介紹了python實(shí)現(xiàn)pymysql數(shù)據(jù)庫(kù)操作方法,文章基于python的相關(guān)內(nèi)容展開對(duì)?pymysql?數(shù)據(jù)庫(kù)操作方法的詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04
Python多進(jìn)程同步簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了Python多進(jìn)程同步簡(jiǎn)單實(shí)現(xiàn)代碼,涉及Python基于Process與Lock模塊運(yùn)行進(jìn)程與鎖機(jī)制實(shí)現(xiàn)多進(jìn)程同步的相關(guān)技巧,需要的朋友可以參考下2016-04-04
python使用wxpy實(shí)現(xiàn)微信消息防撤回腳本
這篇文章主要為大家詳細(xì)介紹了python使用wxpy實(shí)現(xiàn)微信消息防撤回腳本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
python實(shí)現(xiàn)可將字符轉(zhuǎn)換成大寫的tcp服務(wù)器實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)可將字符轉(zhuǎn)換成大寫的tcp服務(wù)器,通過tcp服務(wù)器端實(shí)現(xiàn)針對(duì)字符的轉(zhuǎn)換與返回功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
Python計(jì)算素?cái)?shù)個(gè)數(shù)的兩種方法
本文主要介紹了Python計(jì)算素?cái)?shù)個(gè)數(shù)的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05

