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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 开发api_使用FastAPI和Python快速开发高性能API

發(fā)布時(shí)間:2023/11/29 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 开发api_使用FastAPI和Python快速开发高性能API 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python 開發(fā)api

If you have read some of my previous Python articles, you know I’m a Flask fan. It is my go-to for building APIs in Python. However, recently I started to hear a lot about a new API framework for Python called FastAPI. After building some APIs with it, I can say it is amazing!

如果您閱讀過我以前的一些Python文章,您就會(huì)知道我是Flask的粉絲。 這是我用Python構(gòu)建API的必修課。 但是,最近我開始聽到很多關(guān)于Python的新API框架FastAPI的信息 。 用它構(gòu)建了一些API之后,我可以說它很棒!

This project was created by Sebastian Ramírez, and at the time of writing, it has accumulated almost 20K stars. Big names like Microsoft, Uber, Netflix, and others have been building APIs with it.

這個(gè)項(xiàng)目是由塞巴斯蒂安·拉米雷斯 ( SebastianRamírez)創(chuàng)建的,在撰寫本文時(shí),它已經(jīng)積累了近2萬顆星。 像Microsoft,Uber,Netflix等大公司都在使用它來構(gòu)建API。

But why is this new library so popular and how does it compare to Flask or Django?

但是,為什么這個(gè)新庫如此受歡迎?與Flask或Django相比,它又如何呢?

特征 (Features)

FastAPI is a rather minimalistic framework. However, that doesn’t make it less powerful. FastAPI is built using modern Python concepts and is based on Python 3.6 type declarations. Let’s see some of the features this library is packed with.

FastAPI是一個(gè)相當(dāng)簡單的框架。 但是,這并不會(huì)使它的功能降低。 FastAPI是使用現(xiàn)代Python概念構(gòu)建的,并且基于Python 3.6類型聲明。 讓我們看一下該庫附帶的一些功能。

自動(dòng)文檔 (Automatic docs)

A must-have for any API is documentation about the endpoints and types. A common approach to solve this problem is the use of OpenAPI and tools like Swagger UI or ReDoc to present the information. These come packed automatically with FastAPI, allowing you to focus more on your code than setting up tools.

任何API的必備組件都是有關(guān)端點(diǎn)和類型的文檔。 解決此問題的常用方法是使用OpenAPI和諸如Swagger UI或ReDoc之類的工具來顯示信息。 這些都與FastAPI自動(dòng)打包在一起,使您可以比設(shè)置工具更專注于代碼。

鍵入Python (Typed Python)

This is a big one: FastAPI makes use of Python 3.6 type declarations (thanks to Pydantic). This means that it uses a Python feature that allows you to specify the type of a variable. And this framework makes extensive use out of it, providing you with great editor support. Autocompletion works amazingly well.

這是一個(gè)很大的問題:FastAPI使用Python 3.6類型聲明(感謝Pydantic)。 這意味著它使用Python功能,可讓您指定變量的類型。 并且該框架對其進(jìn)行了廣泛使用,為您提供了強(qiáng)大的編輯器支持。 自動(dòng)補(bǔ)全效果非常好。

Here is some sample code using typed declarations:

這是一些使用類型聲明的示例代碼:

We just went from:

我們剛從:

name

to:

至:

name: str

That’s it! And as a result:

而已! 結(jié)果是:

Autocompletion in FastAPI using PyCharm使用PyCharm在FastAPI中自動(dòng)完成

Beautiful!

美麗!

驗(yàn)證方式 (Validation)

Validation is already integrated into this framework thanks to Pydantic. You can validate standard Python types as well as some custom field validations. Here are a few examples:

由于Pydantic,驗(yàn)證已集成到此框架中。 您可以驗(yàn)證標(biāo)準(zhǔn)Python類型以及一些自定義字段驗(yàn)證。 這里有一些例子:

  • JSON objects (dict)

    JSON對象( dict )

  • JSON array (list)

    JSON數(shù)組( list )

  • String (str) with min and max lengths

    最小長度和最大長度的字符串( str )

  • Numbers (int, float) with min and max values

    帶有最小值和最大值的數(shù)字( int , float )

  • URL

    網(wǎng)址
  • Email

    電子郵件
  • UUID

    UUID
  • And many more…

    還有很多…

安全與認(rèn)證 (Security and authentication)

This is a crucial part of any API and it’s code that we usually just repeat, so why not integrate much of it into the framework? FastAPI does exactly that.

這是任何API的關(guān)鍵部分,我們通常只重復(fù)其中的代碼,那么為什么不將其大量集成到框架中呢? FastAPI正是這樣做的。

The library provides support for the following:

該庫提供以下支持:

  • HTTP Basic

    HTTP基本
  • OAuth2 (JWT tokens)

    OAuth2(JWT令牌)
  • API keys in headers, query params, or cookies.

    標(biāo)頭,查詢參數(shù)或cookie中的API密鑰。

文獻(xiàn)資料 (Documentation)

This is perhaps not exactly a feature of the framework, but it is worth mentioning. The documentation of the project is simply amazing. It’s very clear and covers topics with examples and explanations.

這也許不是框架的確切功能,但是值得一提。 該項(xiàng)目的文檔簡直太神奇了。 這非常清楚,并通過示例和解釋涵蓋了主題。

性能 (Performance)

FastAPI is fast! It is not only fast to code, but it also processes requests super fast! You can check the benchmarks across multiple frameworks using the TechEmpower benchmark tool. Here are the results I got for Python frameworks. Flask and Django are way behind on the list, with FastAPI being first and thus the most performant:

FastAPI很快! 它不僅可以快速編碼,而且還可以超快地處理請求! 您可以使用TechEmpower基準(zhǔn)測試工具來檢查多個(gè)框架中的基準(zhǔn) 。 這是我為Python框架獲得的結(jié)果。 Flask和Django排在后面,FastAPI排名第一,因此也是性能最高的:

TechEmpower benchmark resultsTechEmpower基準(zhǔn)測試結(jié)果

自然異步 (Asynchronous by Nature)

Let’s take a look at the following code:

讓我們看下面的代碼:

@app.post("/item/", response_model=Item)
async def create_item(item: Item):
result = await some_code_running_in_background(item)
return result

Is that JavaScript? I promise you it is not. Looks familiar, though, right? That snippet is actually Python using async methods.

那是JavaScript嗎? 我向你保證不是。 看起來很熟悉,對吧? 該片段實(shí)際上是使用異步方法的Python。

FastAPI supports asynchronous endpoints by default, which can simplify and make your code more efficient. This is a huge advantage over Flask. Django already made some async support work, but it is not as integrated as it is in FastAPI.

FastAPI默認(rèn)情況下支持異步端點(diǎn),這可以簡化并提高代碼效率。 與Flask相比,這是一個(gè)巨大的優(yōu)勢。 Django已經(jīng)進(jìn)行了一些異步支持工作,但是集成程度不如FastAPI。

結(jié)論 (Conclusion)

FastAPI is a relatively new framework that follows the minimalist approach of Flask but adds crucial features that make it easier to work with and stunningly performant. It is a great choice for your next API project, and I will be writing more about it as I use it more and more on my APIs.

FastAPI是一個(gè)相對較新的框架,它遵循Flask的極簡主義方法,但增加了一些關(guān)鍵功能,使其更易于使用且性能驚人。 對于您的下一個(gè)API項(xiàng)目來說,這是一個(gè)不錯(cuò)的選擇,隨著我在API上越來越多地使用它,我將對此進(jìn)行更多的寫作。

Thanks for reading!

謝謝閱讀!

翻譯自: https://medium.com/better-programming/quickly-develop-highly-performant-apis-with-fastapi-and-python-4ac1f252c935

python 開發(fā)api

總結(jié)

以上是生活随笔為你收集整理的python 开发api_使用FastAPI和Python快速开发高性能API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。