zhaizz Blog

website·Indexed 31 days ago

https://blog.zhaizz.top/

A personal blog featuring technical guides and notes, including instructions on manually stopping or starting VMware background services via batch files.

Tokens
663
Snippets
7
Records
7
Agent score
50%

What's inside zhaizz blog

  1. Set up a FastAPI development environment

    FastAPI requires Python 3.8+. It is recommended to use a virtual environment to avoid polluting the system Python installation. Installing the fastapi[standard] package includes uvicorn (the ASGI server) and other essential development dependencies.
    mkdir fastapi-demo && cd fastapi-demo
    python3 -m venv venv
    source venv/bin/activate # Windows: venv\Scripts\activate
    pip install "fastapi[standard]"
  2. Manually stop or start VMware background services on Windows

    You can use a batch file to stop VMware background services when the software is not in use to free up system resources. Warning: Do not stop these services while a virtual machine is running, as it may disrupt network connectivity and USB device functionality.
    @echo off
    net stop VMwareHostd
    net stop VMU
  3. Handle form data and file uploads in FastAPI

    To use Form parameters in FastAPI, the python-multipart library must be installed. This is required for parsing form-encoded data, which is commonly used for login interfaces (e.g., OAuth2 password flow) where credentials must be sent as form fields rather than JSON.
    pip install python-multipart
  4. Validate query parameters in FastAPI using Annotated and Query

    Use Annotated combined with Query to add constraints to optional query parameters, such as limiting the maximum length of a string.
    from typing import Annotated
    from fastapi import FastAPI, Query
    
    @app.get("/items/")
    async def read_items(q: Annotated[str | None, Query(max_length=50)] = None):
        return {"q": q}
  5. Implement middleware in FastAPI

    Middleware in FastAPI is a function that runs before every request is processed by any path operation and after every response is generated. It can be used to add custom headers or track processing time.
    import time
    from fastapi import FastAPI, Request
    
    app = FastAPI()
    
    @app.middleware("http")
    async def add_process_time_header(request: Request, call_next):
        start = time.perf_counter()
        response = await call_next(request)
        # process response here
        return response
  6. Define response models in FastAPI

    Adding a return type annotation to a path operation function tells FastAPI to use that type as the response model. This automatically handles data validation, JSON Schema generation for documentation, serialization via Pydantic, and filtering of output fields.
    from pydantic import BaseModel
    
    class Item(BaseModel):
        name: str
        price: float
    
    @app.post("/items/")
    async def create_item(item: Item) -> Item:
        return item
  7. Check and modify npm global prefix and cache paths on Windows

    To manage where npm installs global packages and stores cache data on Windows, you can inspect the current configuration using the config ls command. The prefix attribute defines the installation path for global packages and their CLI scripts, while cache defines the location for downloaded module cache.
    npm config ls -l