from fastapi import FastAPI from fastapi.responses import HTMLResponse import os import threading from datetime import datetime from ads import AsyncDataSet import uvicorn app = FastAPI() # Initialize AsyncDataSet db = AsyncDataSet(":memory:", socket_path="test_workers.sock") @app.get("/write") async def write_data(): """Write process and thread ID to database.""" process_id = os.getpid() thread_id = threading.get_ident() timestamp = datetime.now().isoformat() # Insert data with process and thread info await db.insert( "requests", { "process_id": process_id, "thread_id": thread_id, "timestamp": timestamp, "endpoint": "/write", }, ) return { "status": "success", "process_id": process_id, "thread_id": thread_id, "timestamp": timestamp, } @app.get("/data", response_class=HTMLResponse) async def get_all_data(): """Return all request data as HTML table.""" # Get all records ordered by timestamp records = await db.find("requests", order_by="timestamp DESC") # Build HTML table html = """ Worker Test Results

AsyncDataSet Multi-Worker Test Results

""" # Calculate statistics if records: unique_processes = len(set(r["process_id"] for r in records)) unique_threads = len(set((r["process_id"], r["thread_id"]) for r in records)) total_requests = len(records) html += f"""
Statistics:
Total Requests: {total_requests}
Unique Processes: {unique_processes}
Unique Process-Thread Combinations: {unique_threads}
""" html += """ """ for record in records: html += f""" """ html += """
ID Process ID Thread ID Timestamp Endpoint
{record.get('uid', 'N/A')[:8]}... {record.get('process_id', 'N/A')} {record.get('thread_id', 'N/A')} {record.get('timestamp', 'N/A')} {record.get('endpoint', 'N/A')}

Make a write request | Refresh

""" return html @app.get("/") async def root(): """Root endpoint showing process and thread info.""" process_id = os.getpid() thread_id = threading.get_ident() # Log this request too await db.insert( "requests", { "process_id": process_id, "thread_id": thread_id, "timestamp": datetime.now().isoformat(), "endpoint": "/", }, ) return { "message": "FastAPI Multi-Worker Test Server", "current_process_id": process_id, "current_thread_id": thread_id, "endpoints": { "/": "This endpoint", "/write": "Write process/thread data (POST)", "/data": "View all data as HTML table (GET)", }, } if __name__ == "__main__": # Run with multiple workers # You can also run this from command line: # uvicorn test_server:app --workers 4 --host 0.0.0.0 --port 8000 uvicorn.run( "ads_test_server:app", host="0.0.0.0", port=8000, workers=50, # Run with 4 worker processes reload=False, )