Replies: 1 comment
-
|
I will attempt to make an unbiased comparison, of course I encourage to take this comparison The two packages have some similarity because I believe initially the purpose of this package was to be a simplified replacement for Relevant versions:
OverviewBoth packages have a similar API with a global Under the hood implementation is very different for both packages (if that's relevant for you), but they support The major difference is that Major differences
Minor differences
Similarities
PerformanceIf performance of the DI matters to you, then My system specs: Benchmarks
import asyncio
import time
from dataclasses import dataclass
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject
@dataclass(frozen=True)
class Repo:
dsn: str
@dataclass(frozen=True)
class Service:
repo: Repo
def query_sync(self) -> str:
return f"sync:{self.repo.dsn}"
async def query_async(self) -> str:
await asyncio.sleep(0)
return f"async:{self.repo.dsn}"
class Container(containers.DeclarativeContainer):
wiring_config = containers.WiringConfiguration(modules=[__name__])
repo = providers.Singleton(Repo, dsn="sqlite:///example.db")
service = providers.Factory(Service, repo=repo)
@inject
def handler_sync(service: Service = Provide[Container.service]) -> str:
return service.query_sync()
@inject
async def handler_async(service: Service = Provide[Container.service]) -> str:
return await service.query_async()
def bench_sync(label: str, fn, *, iters: int) -> None:
t0 = time.perf_counter()
out = None
for _ in range(iters):
out = fn()
dt = time.perf_counter() - t0
print(f"{label:28s} {dt:.6f}s ({iters / dt:,.0f} ops/s) last={out!r}")
async def bench_async(label: str, fn, *, iters: int) -> None:
t0 = time.perf_counter()
out = None
for _ in range(iters):
out = await fn()
dt = time.perf_counter() - t0
print(f"{label:28s} {dt:.6f}s ({iters / dt:,.0f} ops/s) last={out!r}")
async def main() -> None:
iters_sync = 100_000
iters_async = 100_000
t0 = time.perf_counter()
container = Container()
container.wire(modules=[__name__])
build_dt = time.perf_counter() - t0
print(f"container_build+wire {build_dt:.6f}s")
bench_sync("handler_sync (@inject)", handler_sync, iters=iters_sync)
await bench_async("handler_async (@inject)", handler_async, iters=iters_async)
container.unwire()
if __name__ == "__main__":
asyncio.run(main())
import asyncio
import time
from dataclasses import dataclass
from that_depends import BaseContainer, providers, Provide, inject
@dataclass(frozen=True)
class Repo:
dsn: str
@dataclass(frozen=True)
class Service:
repo: Repo
def query_sync(self) -> str:
return f"sync:{self.repo.dsn}"
async def query_async(self) -> str:
await asyncio.sleep(0)
return f"async:{self.repo.dsn}"
class Container(BaseContainer):
repo = providers.Singleton(Repo, dsn="sqlite:///example.db")
service = providers.Factory(Service, repo=repo)
@inject
def handler_sync(service: Service = Provide[Container.service]) -> str:
return service.query_sync()
@inject
async def handler_async(service: Service = Provide[Container.service]) -> str:
return await service.query_async()
def bench_sync(label: str, fn, *, iters: int) -> None:
t0 = time.perf_counter()
out = None
for _ in range(iters):
out = fn()
dt = time.perf_counter() - t0
print(f"{label:28s} {dt:.6f}s ({iters / dt:,.0f} ops/s) last={out!r}")
async def bench_async(label: str, fn, *, iters: int) -> None:
t0 = time.perf_counter()
out = None
for _ in range(iters):
out = await fn()
dt = time.perf_counter() - t0
print(f"{label:28s} {dt:.6f}s ({iters / dt:,.0f} ops/s) last={out!r}")
async def main() -> None:
iters_sync = 300_000
iters_async = 100_000
t0 = time.perf_counter()
build_dt = time.perf_counter() - t0
print(f"container_build+wire {build_dt:.6f}s")
bench_sync("handler_sync (@inject)", handler_sync, iters=iters_sync)
await bench_async("handler_async (@inject)", handler_async, iters=iters_async)
if __name__ == "__main__":
asyncio.run(main())Results
MiscOther, perhaps relevant comparisons. Information collected on the 18.01.2026
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I do not intend to dismiss your work; however, could you clarify how this package differs from python-dependency-injector? After reviewing the documentation for your project, it appears to share some similarities with the package I mentioned. I would appreciate an explanation of the key distinctions between the two.
To reiterate, this question is asked in good faith, and I recognize that your project may offer unique features or design choices.
Beta Was this translation helpful? Give feedback.
All reactions