python Tornado框架的使用示例
Tornado是一個python的開源web框架,它比django要輕量級到多,也沒有什么組件,只有運(yùn)用到對應(yīng)到業(yè)務(wù)場景下我才使用這個框架,它是單進(jìn)程單線程到異步非阻塞模型,適用與長連接長輪巡,高并發(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ù) 必須定義一個executor的屬性,然后run_on_executor裝飾器才會有用。
@run_on_executor # 在這個方法下,線程內(nèi)運(yùn)行;query函數(shù)被run_on_executor包裹(語法糖),將該函數(shù)的執(zhí)行傳遞給線程池executor的線程執(zhí)行,優(yōu)化了處理耗時性任務(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來實現(xiàn)異步阻塞請求
r = {'status': 'True', '線程id': '%s' % big_list[uid], "count": all_count}
self.write(tornado.escape.json_encode(r)) # 寫入返回信息寫入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): # 重寫構(gòu)造方法
# 指定路由規(guī)則
handlers = urls
# 指定配置文件
settings = configs
super(CustomApplication, self).__init__(handlers=handlers, **settings)
# 定義服務(wù)
def create_server():
# 允許在命令行中啟動
#tornado.options.parse_command_line()
# 創(chuàng)建http服務(wù)
http_server = tornado.httpserver.HTTPServer(
CustomApplication() # 注意要實例化
)
# 綁定監(jiān)聽的端口
http_server.listen(options.port)
# 啟動輸入輸出事件循環(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框架的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Python實現(xiàn)web網(wǎng)頁內(nèi)容爬取的方法
在日常學(xué)習(xí)和工作中,我們經(jīng)常會遇到需要爬取網(wǎng)頁內(nèi)容的需求,今天就如何基于Python實現(xiàn)web網(wǎng)頁內(nèi)容爬取進(jìn)行講解,感興趣的朋友一起看看吧2024-12-12
python 解決數(shù)據(jù)庫寫入時float自動變?yōu)檎麛?shù)的問題
這篇文章主要介紹了python 解決數(shù)據(jù)庫寫入時float自動變?yōu)檎麛?shù)的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
tensorflow訓(xùn)練中出現(xiàn)nan問題的解決
本篇文章主要介紹了tensorflow訓(xùn)練中出現(xiàn)nan問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
python應(yīng)用Axes3D繪圖(批量梯度下降算法)
這篇文章主要為大家詳細(xì)介紹了python應(yīng)用Axes3D繪圖,批量梯度下降算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
Python pandas遍歷行數(shù)據(jù)的2種方法小結(jié)
pandas在數(shù)據(jù)處理過程中,除了對整列字段進(jìn)行處理之外,有時還需求對每一行進(jìn)行遍歷,本文就來介紹Python pandas遍歷行數(shù)據(jù)的2種方法小結(jié),感興趣的可以了解一下2024-03-03

