Skip to content

Instantly share code, notes, and snippets.

@KiraPC
Last active May 17, 2023 06:19
Show Gist options
  • Save KiraPC/5016ecee2ae1dd6e860b4494415dbd49 to your computer and use it in GitHub Desktop.
Save KiraPC/5016ecee2ae1dd6e860b4494415dbd49 to your computer and use it in GitHub Desktop.
Uvicorn TCP memory leak
FROM python:3.8
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
RUN pip install "uvicorn[standard]"
COPY main.py /app
EXPOSE 8080
ENTRYPOINT ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
FROM python:3.8
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
RUN pip install "uvicorn[standard]==0.13.4"
COPY main.py /app
EXPOSE 8080
ENTRYPOINT ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
import gc
import asyncio
import logging
from fastapi import FastAPI
from memory_profiler import memory_usage
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger("test.mem")
logger.setLevel(logging.INFO)
app = FastAPI(
title="MemTest",
description="MemTest",
version="0.0.1",
)
async def print_mem():
while True:
await asyncio.sleep(2)
mem_usage = memory_usage(-1, interval=0.2, timeout=1)
logger.info(mem_usage)
logger.info(f"Number of unreachable objects {gc.collect()}")
@app.on_event("startup")
async def on_startup():
asyncio.gather(print_mem())
fastapi==0.70.0
memory_profiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment