-
-
Save KiraPC/5016ecee2ae1dd6e860b4494415dbd49 to your computer and use it in GitHub Desktop.
Uvicorn TCP memory leak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fastapi==0.70.0 | |
memory_profiler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment