Skip to content

Instantly share code, notes, and snippets.

@horpto
Created June 15, 2025 13:25
Show Gist options
  • Save horpto/8d4a020ac9b86e415c3b3a896f3f3d45 to your computer and use it in GitHub Desktop.
Save horpto/8d4a020ac9b86e415c3b3a896f3f3d45 to your computer and use it in GitHub Desktop.
import asyncio
from typing import Any
from time import perf_counter
from fast_depends import Depends, inject
from fast_depends.library import CustomField
class Header(CustomField):
def use(self, /, **kwargs: dict[str, Any]) -> dict[str, Any]:
kwargs = super().use(**kwargs)
kwargs[self.param_name] = kwargs["headers"][self.param_name]
return kwargs
async def get_dep():
return "header"
@inject
async def simple_func(dep: str = Depends(get_dep)):
return dep
@inject
async def custom_func(header_field: str = Header()):
return header_field
async def not_custom(n=1000):
start_t = perf_counter()
for _i in range(n):
await simple_func()
end_t = perf_counter()
print("not_custom", end_t - start_t)
async def with_custom(n=1000):
headers = {"header_field": "1"}
start_t = perf_counter()
for _i in range(n):
await custom_func(headers=headers)
end_t = perf_counter()
print("with_custom", end_t - start_t)
async def amain():
n = 1000
await not_custom(n)
await with_custom(n)
if __name__ == "__main__":
asyncio.run(amain())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment