Slackbot
02/09/2024, 3:11 AMElijah Ben Izzy
02/09/2024, 3:14 AMdef session() -> Session:
... # load it up -- feel free to declare parameters as inputs
Then you can include these in a resources
module which you’d add to your driver. The cool thing is that, if you want to read from another db source, you could always swap out a prod_resources
with.a dev_resources
module in the driver!
Does this help? There are other ways to achieve what you want as well, but this is a pattern we’ve found that people really like.
`Then you often include these in aManabu Niseki
02/09/2024, 3:24 AMimport contextlib
import typing
import requests
@contextlib.contextmanager
def session() -> typing.Generator[requests.Session, None, None]:
with requests.Session() as session:
yield session
async def a(input: pd.Series, session: requests.Session) -> pd.Series:
...
To close a session gracefully.
But the code throws an error ValueError: Error: a is expecting session:<class 'requests.sessions.Session'>, but found session:typing.Generator[requests.sessions.Session, NoneType, NoneType]
.
Is there a workaround?Elijah Ben Izzy
02/09/2024, 3:47 AMElijah Ben Izzy
02/09/2024, 4:08 AMManabu Niseki
02/09/2024, 4:11 AMDepends
in FastAPI and a dependency is not defined in a lifespan.Elijah Ben Izzy
02/09/2024, 4:15 AMdependency
in FastAPI
?Manabu Niseki
02/09/2024, 4:36 AMElijah Ben Izzy
02/09/2024, 4:38 AMManabu Niseki
02/09/2024, 4:49 AMElijah Ben Izzy
02/09/2024, 4:53 AMfrom hamilton.plugins import h_async
async def common_parameters(...) ->
async_driver = ...h_async.AsyncDriver({}, modules) # async driver in async func.
@app.get("/items/")
async def read_items(commons: Annotated[dict, Depends(common_parameters)]):
result = await async_driver.execute([...], inputs={"session": commons["session"]})
# can close/do whatever with session
return result
E.G. just rely on fastAPI to compute the dependencies and pass them in as an input to Hamilton?Manabu Niseki
02/09/2024, 4:56 AMinputs
. It's my bad and now I'm feel I'm very stupid.Elijah Ben Izzy
02/09/2024, 5:00 AMThierry Jean
02/09/2024, 2:13 PMdriver.execute()
within the scope of the context manager and pass the session as input. Something like this:
# my_functions.py
def a(session: requests.Session) -> int: ...
# run.py
@contextlib.contextmanager
def session() -> typing.Generator[requests.Session, None, None]:
with requests.Session() as session:
yield session
dr = driver.Builder().with_modules(my_functions).build()
with session(...) as my_session:
results = dr.execute(["a"], inputs=dict(session=my_session))
print(results)
We have more info about Hamilton + FastAPI in the docs
FastAPI has advanced features for context managers and database connections , using Depends
, but it might be overkill as a first stepManabu Niseki
02/10/2024, 12:33 AMThierry Jean
02/10/2024, 12:43 AM