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

歡迎訪問 生活随笔!

生活随笔

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

python

python数据处理框架_python 最快 web 框架 Sanci 快速入门

發布時間:2025/3/11 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python数据处理框架_python 最快 web 框架 Sanci 快速入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

Sanic 是一個和類Flask 的基于Python3.5+的web框架,它編寫的代碼速度特別快。

除了像Flask 以外,Sanic 還支持以異步請求的方式處理請求。這意味著你可以使用新的 async/await 語法,編寫非阻塞的快速的代碼。

關于 asyncio 包的介紹,請參考之前的一篇文章 python并發2:使用asyncio處理并發

既然它說速度特別快,我們先看下官方提供的 基準測試結果。

Sanic基準測試

這個測試的程序運行在 AWS 實例上,系統是Ubuntu,只使用了一個進程。

那我們就有必要看下uvloop是個什么庫。

uvloop

uvloop 是 asyncio 默認事件循環的替代品,實現的功能完整,切即插即用。uvloop是用CPython 寫的,建于libuv之上。

uvloop 可以使 asyncio 更快。事實上,它至少比 nodejs、gevent 和其他 Python 異步框架要快兩倍 。基于 uvloop 的 asyncio 的速度幾乎接近了 Go 程序的速度。

安裝 uvloop

uvloop 還只能在 *nix 平臺 和 Python3.5+以上版本使用。

使用pip安裝:

pip install uvloop

在 asyncio 代碼中使用uvloop 也很簡單:

import asyncio

import uvloop

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

這得代碼使得對任何asyncio.get_event_loop() 的調用都將返回一個uvloop實例。

現在我們開始學習Sanic:

安裝 Sanic

pip install sanic

創建第一個 sanic 代碼

from sanic import Sanic

from sanic.response import text

app = Sanic(__name__)

@app.route("/")

async def test(request):

return text('Hello world!')

app.run(host="0.0.0.0", port=8000, debug=True)

運行代碼: python main.py, 現在打開瀏覽器訪問 http://0.0.0.0:8000,你會看到 hello world!。

如果你熟悉Flask,你會發現,這個語法簡直和Flask一模一樣。

路由(Routing)

路由用于把一個函數綁定到一個 URL。下面是一些基本的例子:

@app.route('/')

def index():

return text('Index Page')

@app.route('/hello')

def hello():

return text('Hello World')

當然,你還可以動態的變化URL的某些部分,還可以為一個函數指定多個規則。

變量規則

通過把 URL 的一部分標記為 就可以在 URL 中添加變量。標記的 部分會作為關鍵字參數傳遞給函數。通過使用 ,可以 選擇性的加上一個轉換器,為變量指定特定的類型,如果傳入的類型錯誤,Sanic會拋出NotFound異常。請看下面的例子:

from sanic.response import text

@app.route('/tag/')

async def tag_handler(request, tag):

return text('Tag - {}'.format(tag))

@app.route('/number/')

async def integer_handler(request, integer_arg):

return text('Integer - {}'.format(integer_arg))

@app.route('/number/')

async def number_handler(request, number_arg):

return text('Number - {}'.format(number_arg))

@app.route('/person/')

async def person_handler(request, name):

return text('Person - {}'.format(name))

@app.route('/folder/')

async def folder_handler(request, folder_id):

return text('Folder - {}'.format(folder_id))

HTTP 請求類型

默認情況下,我們定義的URL只支持GET 請求,@app.route裝飾器提供了一個可選參數methods,這個參數允許傳入所有HTTP 方法。

例如:

from sanic.response import text

@app.route('/post', methods=['POST'])

async def post_handler(request):

return text('POST request - {}'.format(request.json))

@app.route('/get', methods=['GET'])

async def get_handler(request):

return text('GET request - {}'.format(request.args))

也可以簡寫為:

from sanic.response import text

@app.post('/post')

async def post_handler(request):

return text('POST request - {}'.format(request.json))

@app.get('/get')

async def get_handler(request):

return text('GET request - {}'.format(request.args))

add_route 方法

除了@app.route裝飾器,Sanic 還提供了 add_route 方法。

@app.route 只是包裝了 add_route方法。

from sanic.response import text

# Define the handler functions

async def handler1(request):

return text('OK')

async def handler2(request, name):

return text('Folder - {}'.format(name))

async def person_handler2(request, name):

return text('Person - {}'.format(name))

# Add each handler function as a route

app.add_route(handler1, '/test')

app.add_route(handler2, '/folder/')

app.add_route(person_handler2, '/person/', methods=['GET'])

URL 構建

如果可以匹配URL,那么Sanic可以生成URL嗎?當然可以,url_for() 函數就是用于構建指定函數的URL的。它把函數名稱作為第一個參數,其余參數對應URL中的變量,例如:

@app.route('/')

async def index(request):

# generate a URL for the endpoint `post_handler`

url = app.url_for('post_handler', post_id=5)

# the URL is `/posts/5`, redirect to it

return redirect(url)

@app.route('/posts/')

async def post_handler(request, post_id):

return text('Post - {}'.format(post_id))

未定義變量會作為URL的查詢參數:

url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')

# /posts/5?arg_one=one&arg_two=two

# 支持多值參數

url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])

# /posts/5?arg_one=one&arg_one=two

使用藍圖(Blueprint)

Sanic也提供了和Flask 類似的 Blueprint。

Blueprint有以下用途:

把一個應用分解為一套藍圖。這是針對大型應用的理想方案:一個項目可以實例化一個 應用,初始化多個擴展,并注冊許多藍圖。

在一個應用的 URL 前綴和(或)子域上注冊一個藍圖。 URL 前綴和(或)子域的參數 成為藍圖中所有視圖的通用視圖參數(缺省情況下)。

使用不同的 URL 規則在應用中多次注冊藍圖。

通過藍圖提供模板過濾器、靜態文件、模板和其他工具。藍圖不必執行應用或視圖 函數。

blueprint 示例

from sanic import Sanic

from sanic.response import json

from sanic import Blueprint

bp = Blueprint('my_blueprint')

@bp.route('/')

async def bp_root(request):

return json({'my': 'blueprint'})

app = Sanic(__name__)

app.blueprint(bp)

app.run(host='0.0.0.0', port=8000, debug=True)

Sanic 使用 app.blueprint() 方法注冊blueprint。

使用藍圖注冊全局中間件

@bp.middleware

async def print_on_request(request):

print("I am a spy")

@bp.middleware('request')

async def halt_request(request):

return text('I halted the request')

@bp.middleware('response')

async def halt_response(request, response):

return text('I halted the response')

使用藍圖處理異常

@bp.exception(NotFound)

def ignore_404s(request, exception):

return text("Yep, I totally found the page: {}".format(request.url))

使用藍圖處理靜態文件

第一個參數指向當前的Python包

第二個參數是靜態文件的目錄

bp.static('/folder/to/serve', '/web/path')

使用url_for

如果要創建頁面鏈接,可以和通常一樣使用 url_for() 函數,只是要把藍圖名稱作為端點的前綴,并且用一個點( . )來 分隔:

@blueprint_v1.route('/')

async def root(request):

url = app.url_for('v1.post_handler', post_id=5) # --> '/v1/post/5'

return redirect(url)

@blueprint_v1.route('/post/')

async def post_handler(request, post_id):

return text('Post {} in Blueprint V1'.format(post_id))

操作請求數據

對于web 應用來說對客戶端向服務器發送的數據做出相應很重要,在Sanic中由傳入的參數 request來提供請求信息。

為什么不像Flask 一樣提供一個全局變量 request?

Flask 是同步請求,每次請求都有一個獨立的新線程來處理,這個線程中也只處理這一個請求。而Sanic是基于協程的處理方式,一個線程可以同時處理幾個、幾十個甚至幾百個請求,把request作為全局變量顯然會比較難以處理。

Request 對象常用參數有

json(any) json body

from sanic.response import json

@app.route("/json")

def post_json(request):

return json({ "received": True, "message": request.json })

args(dict) URL請求參數

?key1=value1&key2=value2 將轉變為

{'key1': ['value1'], 'key2': ['value2']}

raw_args(dict) 和args 類似

?key1=value1&key2=value2 將轉變為

{'key1': 'value1', 'key2': 'value2'}

form(dict)處理 POST 表單請求,數據是一個字典

body(bytes)處理POST 表單請求,數據是一個字符串

其他參數還有:

file

ip

app

url

scheme

path

query_string

關于響應

Sanic使用response 函數創建響應對象。

文本 response.text('hello world')

html response.html('

hello world

')

json response.json({'hello': 'world'})

file response.file('/srv/www/hello.txt')

streaming

from sanic import response

@app.route("/streaming")

async def index(request):

async def streaming_fn(response):

response.write('foo')

response.write('bar')

return response.stream(streaming_fn, content_type='text/plain')

redirect response.file('/json')

raw response.raw('raw data')

如果想修改響應的headers可以傳入headers 參數

from sanic import response

@app.route('/json')

def handle_request(request):

return response.json(

{'message': 'Hello world!'},

headers={'X-Served-By': 'sanic'},

status=200

)

配置管理

應用總是需要一定的配置的。根據應用環境不同,會需要不同的配置。比如開關調試 模式、設置密鑰以及其他依賴于環境的東西。

Sanic 的設計思路是在應用開始時載入配置。你可以在代碼中直接硬編碼寫入配置,也可以使用配置文件。

不管你使用何種方式載入配置,都可以使用 Sanic 的 config 屬性來操作配置的值。 Sanic 本身就使用這個對象來保存 一些配置,擴展也可以使用這個對象保存配置。同時這也是你保存配置的地方。

配置入門

config 實質上是一個字典的子類,可以像字典一樣操作:

app = Sanic('myapp')

app.config.DB_NAME = 'appdb'

app.config.DB_USER = 'appuser'

也可以一次更新多個配置:

db_settings = {

'DB_HOST': 'localhost',

'DB_NAME': 'appdb',

'DB_USER': 'appuser'

}

app.config.update(db_settings)

從對象導入配置

import myapp.default_settings

app = Sanic('myapp')

app.config.from_object(myapp.default_settings)

使用配置文件

如果把配置放在一個單獨的文件中會更有用。理想情況下配置文件應當放在應用包的 外面。這樣可以在修改配置文件時不影響應用的打包與分發

常見用法如下:

app = Sanic('myapp')

app.config.from_envvar('MYAPP_SETTINGS')

首先從 myapp.default_settings 模塊載入配置,然后根據 MYAPP_SETTINGS 環境變量所指向的文件的內容重載配置的值。在 啟動服務器前,在 Linux 或 OS X 操作系統中,這個環境變量可以在終端中使用 export 命令來設置:

$ export MYAPP_SETTINGS=/path/to/config_file

$ python myapp.py

部署

Sanic 項目還不是特別成熟,現在部署比較簡陋。對Gunicorn的支持也不完善。

詳細信息可以 看下這個問題 Projects built with sanic?

先在說下我的部署方式

使用 supervisord 部署

啟動 方式

supervisord -c supervisor.conf

總結

試用了下Sanic,把之前的一個聊天機器人從Flask 改成了 Sanic。不得不說,如果你有Flask經驗,大致看一下Sanic文檔就可以直接上手了。

并且Sanic 的速度比Flask 快很多,只是Sanic配套的包還是太少,用于生產環境有一定的風險。

預告

下一篇將介紹如何使用 Sanic 一步一步創建一個 聊天機器人。

參考鏈接

最后,感謝女朋友支持。

>歡迎關注

>請我喝芬達

彩蛋

魔魔是我們家巴哥的名字

貼一張魔魔的照片結束本篇文章。

總結

以上是生活随笔為你收集整理的python数据处理框架_python 最快 web 框架 Sanci 快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。

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