Posts

Showing posts from January 10, 2024

FastAPI with async

 In FastAPI , using ` async ` functions is optional, but it provides certain advantages, especially in scenarios where your application may need to handle multiple concurrent operations without blocking the execution of other tasks. The use of `async` is closely tied to asynchronous programming in Python, often referred to as asyncio. Here are some reasons why you might choose to use `async` functions in FastAPI: 1. Concurrent Operations:    - Async functions allow your application to handle multiple operations concurrently without waiting for each operation to complete before moving on to the next one.    - This can be beneficial for I/O-bound operations like making multiple API requests, database queries, or other network-related tasks. 2. Improved Performance:    - Asynchronous programming can improve the overall performance of your application, especially in scenarios where there are many I/O-bound tasks.    - Instead of waiting for one t...

Fast API with Pydentic

FastAPI  and Pydantic are often used together to build APIs in Python. FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. Pydantic is a data validation and settings management library that plays well with FastAPI. Here's a simple example: Let's create an API using FastAPI with Pydantic for request and response models. ```python from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() # Pydantic model for request class Item(BaseModel):     name: str     description: str = None     price: float     quantity: int # Pydantic model for response class ItemResponse(BaseModel):     name: str     description: str = None # Endpoint to create an item @app.post("/items/", response_model=ItemResponse) async def create_item(item: Item):     return {"name": item.name, "description": item.description} # Endpoint to read an item by...