Files
hatchet/sdks/python/examples/lifespans/simple.py
Matt Kaye 90805ca763 Feat: Lifespans! (#1541)
* feat: implement lifespans

* feat: add psycopg test dep

* feat: lifespan script

* feat: examples and tests

* drive-by: add license

* drive-by: throw errors for sanity checks

* feat: docs

* feat: sync and async

* fix: docs

* fix: lint

* fix: small tweaks

* fix: rm unused

* fix: typed example

* fix: print
2025-04-14 08:56:39 -04:00

37 lines
674 B
Python

# ❓ Lifespan
from typing import AsyncGenerator, cast
from pydantic import BaseModel
from hatchet_sdk import Context, EmptyModel, Hatchet
hatchet = Hatchet(debug=True)
class Lifespan(BaseModel):
foo: str
pi: float
async def lifespan() -> AsyncGenerator[Lifespan, None]:
yield Lifespan(foo="bar", pi=3.14)
@hatchet.task(name="LifespanWorkflow")
def lifespan_task(input: EmptyModel, ctx: Context) -> Lifespan:
return cast(Lifespan, ctx.lifespan)
def main() -> None:
worker = hatchet.worker(
"test-worker", slots=1, workflows=[lifespan_task], lifespan=lifespan
)
worker.start()
# ‼️
if __name__ == "__main__":
main()