Skip to content

Commit

Permalink
fix: use a directory pool
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Jun 7, 2024
1 parent 3602470 commit 725ba54
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/bentoml/_internal/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import contextlib
import contextvars
import os
import shutil
import tempfile
import typing as t
from abc import ABC
from abc import abstractmethod
Expand All @@ -14,6 +12,7 @@
import starlette.datastructures

from .utils.http import Cookie
from .utils.temp import TempfilePool

if TYPE_CHECKING:
import starlette.requests
Expand All @@ -26,12 +25,14 @@
contextvars.ContextVar("response")
)

request_tempdir_pool = TempfilePool(prefix="bentoml-request-")


def request_temp_dir() -> str:
"""A request-unique directory for storing temporary files"""
request = _request_var.get()
if not hasattr(request.state, "temp_dir"):
request.state.temp_dir = tempfile.mkdtemp(prefix="bentoml-request-")
request.state.temp_dir = request_tempdir_pool.acquire()
return request.state.temp_dir


Expand Down Expand Up @@ -106,7 +107,7 @@ def in_request(
yield self
finally:
if hasattr(request.state, "temp_dir"):
shutil.rmtree(request.state.temp_dir, ignore_errors=True)
request_tempdir_pool.release(request.state.temp_dir)
_request_var.reset(request_token)
_response_var.reset(response_token)

Expand Down
4 changes: 3 additions & 1 deletion src/bentoml/_internal/server/base_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def on_startup(self) -> list[LifecycleHook]:

@property
def on_shutdown(self) -> list[LifecycleHook]:
return []
from ..context import request_tempdir_pool

return [request_tempdir_pool.cleanup]

def mark_as_ready(self) -> None:
self._is_ready = True
Expand Down
45 changes: 45 additions & 0 deletions src/bentoml/_internal/utils/temp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

import shutil
import tempfile
from collections import deque
from functools import partial
from pathlib import Path
from threading import Lock


class TempfilePool:
"""A simple pool to get temp directories
so they are reused as most as possible.
"""

def __init__(
self,
suffix: str | None = None,
prefix: str | None = None,
dir: str | None = None,
) -> None:
self._pool: deque[str] = deque([])
self._lock = Lock()
self._new = partial(tempfile.mkdtemp, suffix=suffix, prefix=prefix, dir=dir)

def cleanup(self) -> None:
while len(self._pool):
dir = self._pool.popleft()
shutil.rmtree(dir, ignore_errors=True)

def acquire(self) -> str:
with self._lock:
if not len(self._pool):
return self._new()
else:
return self._pool.popleft()

def release(self, dir: str) -> None:
with self._lock:
for child in Path(dir).iterdir():
if child.is_dir():
shutil.rmtree(child)
else:
child.unlink()
self._pool.append(dir)

0 comments on commit 725ba54

Please sign in to comment.