python Tornado框架的使用示例
Tornado是一個(gè)python的開(kāi)源web框架,它比django要輕量級(jí)到多,也沒(méi)有什么組件,只有運(yùn)用到對(duì)應(yīng)到業(yè)務(wù)場(chǎng)景下我才使用這個(gè)框架,它是單進(jìn)程單線程到異步非阻塞模型,適用與長(zhǎng)連接長(zhǎng)輪巡,高并發(fā),異步非阻塞
安裝:
pip install tornado
View層
'''
@File : views_service.py
@Copyright : rainbol
@Date : 2020/8/31
@Desc :
'''
import threading
import time
import tornado.web
import tornado
import tornado.ioloop
import tornado.web
import tornado.gen
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor
from uuid import uuid4
import random
all_count = 0
big_list = {}
class ServiceHandler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(20) # 最大線程數(shù) 必須定義一個(gè)executor的屬性,然后run_on_executor裝飾器才會(huì)有用。
@run_on_executor # 在這個(gè)方法下,線程內(nèi)運(yùn)行;query函數(shù)被run_on_executor包裹(語(yǔ)法糖),將該函數(shù)的執(zhí)行傳遞給線程池executor的線程執(zhí)行,優(yōu)化了處理耗時(shí)性任務(wù),以致達(dá)到不阻塞主線程的效果。
def time_demo(self, tid, uid):
time.sleep(tid)
threading_id = threading.current_thread().ident
big_list[uid] = threading_id
@tornado.gen.coroutine # 異步、協(xié)程處理;增加并發(fā)量
def post(self):
global all_count
all_count += 1
uid = str(uuid4())
yield self.time_demo(random.randint(1, 100), uid) # 模擬業(yè)務(wù)處理,使用yield來(lái)實(shí)現(xiàn)異步阻塞請(qǐng)求
r = {'status': 'True', '線程id': '%s' % big_list[uid], "count": all_count}
self.write(tornado.escape.json_encode(r)) # 寫(xiě)入返回信息寫(xiě)入response
self.finish() # 結(jié)束服務(wù)
def get(self):
return self.post()
__init__.py
'''
@File : __init__.py
@Copyright : rainbol
@Date : 2020/8/31
@Desc :
'''
import tornado.web # web框架
import tornado.httpserver # http服務(wù)
import tornado.ioloop # 輸入輸出事件循環(huán)
import tornado.options # 配置工具
from tornado.options import options, define
from app.config import configs
from app.urls import urls
define('port', default=8000, type=int, help='運(yùn)行端口')
# 自定義應(yīng)用
class CustomApplication(tornado.web.Application):
def __init__(self): # 重寫(xiě)構(gòu)造方法
# 指定路由規(guī)則
handlers = urls
# 指定配置文件
settings = configs
super(CustomApplication, self).__init__(handlers=handlers, **settings)
# 定義服務(wù)
def create_server():
# 允許在命令行中啟動(dòng)
#tornado.options.parse_command_line()
# 創(chuàng)建http服務(wù)
http_server = tornado.httpserver.HTTPServer(
CustomApplication() # 注意要實(shí)例化
)
# 綁定監(jiān)聽(tīng)的端口
http_server.listen(options.port)
# 啟動(dòng)輸入輸出事件循環(huán)
tornado.ioloop.IOLoop.instance().start()
''' @File : manage.py @Copyright : rainbol @Date : 2020/8/31 @Desc : ''' from app.views import create_server if __name__ == '__main__': create_server()
路由
from app.views.views_index import IndexHandler as index from app.views.views_service import ServiceHandler as service # 配置路由和配置到映射規(guī)則 urls = [ (r"/index", index), (r"/demo", service), ]
以上就是python Tornado框架的使用示例的詳細(xì)內(nèi)容,更多關(guān)于python Tornado框架的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python Request類(lèi)源碼實(shí)現(xiàn)方法及原理解析
這篇文章主要介紹了Python Request類(lèi)源碼實(shí)現(xiàn)方法及原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Linux安裝Pytorch1.8GPU(CUDA11.1)的實(shí)現(xiàn)
這篇文章主要介紹了Linux安裝Pytorch1.8GPU(CUDA11.1)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
基于Pytorch的神經(jīng)網(wǎng)絡(luò)之Regression的實(shí)現(xiàn)
本文主要介紹了基于Pytorch的神經(jīng)網(wǎng)絡(luò)之Regression的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
jupyter notebook出現(xiàn)In[*]的問(wèn)題及解決
這篇文章主要介紹了jupyter notebook出現(xiàn)In[*]的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實(shí)例
下面小編就為大家分享一篇python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
詳解Python list和numpy array的存儲(chǔ)和讀取方法
這篇文章主要介紹了詳解Python list和numpy array的存儲(chǔ)和讀取方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Python深入分析@property裝飾器的應(yīng)用
這篇文章主要介紹了Python @property裝飾器的用法,在Python中,可以通過(guò)@property裝飾器將一個(gè)方法轉(zhuǎn)換為屬性,從而實(shí)現(xiàn)用于計(jì)算的屬性,下面文章圍繞主題展開(kāi)更多相關(guān)詳情,感興趣的小伙伴可以參考一下2022-07-07
使用 django orm 寫(xiě) exists 條件過(guò)濾實(shí)例
這篇文章主要介紹了使用 django orm 寫(xiě) exists 條件過(guò)濾實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05

