python 開發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文章,您就會知道我是Flask的粉絲。 這是我用Python構建API的必修課。 但是,最近我開始聽到很多關于Python的新API框架FastAPI的信息 。 用它構建了一些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.
這個項目是由塞巴斯蒂安·拉米雷斯 ( SebastianRamírez)創建的,在撰寫本文時,它已經積累了近2萬顆星。 像Microsoft,Uber,Netflix等大公司都在使用它來構建API。
But why is this new library so popular and how does it compare to Flask or Django?
但是,為什么這個新庫如此受歡迎?與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是一個相當簡單的框架。 但是,這并不會使它的功能降低。 FastAPI是使用現代Python概念構建的,并且基于Python 3.6類型聲明。 讓我們看一下該庫附帶的一些功能。
自動文檔 (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的必備組件都是有關端點和類型的文檔。 解決此問題的常用方法是使用OpenAPI和諸如Swagger UI或ReDoc之類的工具來顯示信息。 這些都與FastAPI自動打包在一起,使您可以比設置工具更專注于代碼。
鍵入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.
這是一個很大的問題:FastAPI使用Python 3.6類型聲明(感謝Pydantic)。 這意味著它使用Python功能,可讓您指定變量的類型。 并且該框架對其進行了廣泛使用,為您提供了強大的編輯器支持。 自動補全效果非常好。
Here is some sample code using typed declarations:
這是一些使用類型聲明的示例代碼:
We just went from:
我們剛從:
name
to:
至:
name: str
That’s it! And as a result:
而已! 結果是:

Beautiful!
美麗!
驗證方式 (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,驗證已集成到此框架中。 您可以驗證標準Python類型以及一些自定義字段驗證。 這里有一些例子:
JSON objects (
dict
)JSON對象(
dict
)JSON array (
list
)JSON數組(
list
)String (
str
) with min and max lengths最小長度和最大長度的字符串(
str
)Numbers (
int
,float
) with min and max values帶有最小值和最大值的數字(
int
,float
)- URL 網址
- Email 電子郵件
- UUID UUID
- And many more… 還有很多…
安全與認證 (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的關鍵部分,我們通常只重復其中的代碼,那么為什么不將其大量集成到框架中呢? FastAPI正是這樣做的。
The library provides support for the following:
該庫提供以下支持:
- HTTP Basic HTTP基本
- OAuth2 (JWT tokens) OAuth2(JWT令牌)
- API keys in headers, query params, or cookies. 標頭,查詢參數或cookie中的API密鑰。
文獻資料 (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.
這也許不是框架的確切功能,但是值得一提。 該項目的文檔簡直太神奇了。 這非常清楚,并通過示例和解釋涵蓋了主題。
性能 (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基準測試工具來檢查多個框架中的基準 。 這是我為Python框架獲得的結果。 Flask和Django排在后面,FastAPI排名第一,因此也是性能最高的:

自然異步 (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嗎? 我向你保證不是。 看起來很熟悉,對吧? 該片段實際上是使用異步方法的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默認情況下支持異步端點,這可以簡化并提高代碼效率。 與Flask相比,這是一個巨大的優勢。 Django已經進行了一些異步支持工作,但是集成程度不如FastAPI。
結論 (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是一個相對較新的框架,它遵循Flask的極簡主義方法,但增加了一些關鍵功能,使其更易于使用且性能驚人。 對于您的下一個API項目來說,這是一個不錯的選擇,隨著我在API上越來越多地使用它,我將對此進行更多的寫作。
Thanks for reading!
謝謝閱讀!
翻譯自: https://medium.com/better-programming/quickly-develop-highly-performant-apis-with-fastapi-and-python-4ac1f252c935
python 開發api
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/388087.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/388087.shtml 英文地址,請注明出處:http://en.pswp.cn/news/388087.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!