FastAPI is built on top of Starlette for the web parts and Pydantic for data validation. It uses Python type hints to automatically generate OpenAPI schemas and perform data validation. Here is a simple example of a FastAPI application:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
In this example, we create a FastAPI application instance app
and define a simple route /
that returns a JSON response.
Asynchronous programming in Python is based on the concept of async
and await
keywords. An async
function is a special type of function that can pause its execution and allow other tasks to run. The await
keyword is used inside an async
function to wait for the result of another async
function.
import asyncio
async def say_hello():
print("Hello")
await asyncio.sleep(1)
print("World")
async def main():
await say_hello()
asyncio.run(main())
In this example, the say_hello
function is an async
function. The await asyncio.sleep(1)
statement pauses the execution of the say_hello
function for 1 second, allowing other tasks to run during this time.
To create a basic FastAPI application, you need to follow these steps:
pip install fastapi uvicorn
main.py
:from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello, FastAPI!"}
uvicorn main:app --reload
In FastAPI, you can define asynchronous routes by using async
functions. This is useful when your route handler needs to perform I/O operations.
from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get("/async-route")
async def async_route():
await asyncio.sleep(1)
return {"message": "This is an asynchronous route"}
In this example, the /async-route
route pauses its execution for 1 second using await asyncio.sleep(1)
and then returns a JSON response.
When working with databases in a FastAPI application, it’s recommended to use asynchronous database drivers. For example, if you are using PostgreSQL, you can use asyncpg
.
from fastapi import FastAPI
import asyncpg
app = FastAPI()
async def get_db_connection():
conn = await asyncpg.connect(user='user', password='password',
database='mydb', host='127.0.0.1')
return conn
@app.get("/db-data")
async def get_db_data():
conn = await get_db_connection()
try:
rows = await conn.fetch("SELECT * FROM mytable")
return [dict(row) for row in rows]
finally:
await conn.close()
When making external API calls, you can use the aiohttp
library, which is an asynchronous HTTP client/server library.
from fastapi import FastAPI
import aiohttp
app = FastAPI()
@app.get("/external-api")
async def call_external_api():
async with aiohttp.ClientSession() as session:
async with session.get('https://api.example.com/data') as response:
data = await response.json()
return data
Proper error handling is crucial in a FastAPI application. You can use the HTTPException
class to return HTTP errors.
from fastapi import FastAPI, HTTPException
app = FastAPI()
@app.get("/item/{item_id}")
async def read_item(item_id: int):
if item_id < 0:
raise HTTPException(status_code=400, detail="Item ID cannot be negative")
return {"item_id": item_id}
To test asynchronous FastAPI applications, you can use the TestClient
from fastapi.testclient
and the pytest-asyncio
library.
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"Hello": "World"}
FastAPI and asynchronous programming in Python provide a powerful combination for building high - performance and scalable web APIs. By understanding the fundamental concepts, using the right usage methods, following common practices, and implementing best practices, you can build efficient and reliable applications. Whether it’s handling database operations, making external API calls, or dealing with error handling and testing, FastAPI and asynchronous programming offer a wide range of tools and techniques to simplify the development process.