Skip to content

armgit / rembg

Public Imported from GitHub
0

Rembg is a tool to remove images background

[FTR] feature/thread-limiter | Add anyio thread limiter

Aleksandar Ivanovski1c46b4c8f6f5
diff --git a/rembg/cli.py b/rembg/cli.py
index 6210ef7..2d3cebd 100644
--- a/rembg/cli.py
+++ b/rembg/cli.py
@@ -257,7 +257,15 @@ def p(
show_default=True,
help="log level",
)
-def s(port: int, log_level: str) -> None:
+ "-t",
+ "--threads",
+ default=None,
+ type=int,
+ show_default=True,
+ help="number of worker threads",
+)
+def s(port: int, log_level: str, threads: int) -> None:
sessions: dict[str, BaseSession] = {}
tags_metadata = [
{
@@ -384,6 +392,13 @@ def s(port: int, log_level: str) -> None:
media_type="image/png",
)
+ @app.on_event("startup")
+ def startup():
+ if threads is not None:
+ from anyio.lowlevel import RunVar
+ from anyio import CapacityLimiter
+ RunVar("_default_thread_limiter").set(CapacityLimiter(threads))
+
@app.get(
path="/",
tags=["Background Removal"],