Files
hatchet/examples/python/streaming/worker.py
Matt Kaye 2cfb345dcf Feat: Handle unicode error, fix OTel error capturing (#1959)
* feat: raise illegal task output error out of the runner if we get a null unicode escape sequence

* feat: add helper for sanitizing outputs

* feat: improve error

* fix: lock index

* feat: use async stream method in example

* chore: ver

* chore: changelog

* fix: turns out we don't need to lock

* fix: return exception for instrumentation

* chore: changelog

* chore: bunch of generated crap

* fix: comment placement

* fix: copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-07-09 14:18:41 -04:00

44 lines
1.1 KiB
Python

import asyncio
from typing import Generator
from hatchet_sdk import Context, EmptyModel, Hatchet
hatchet = Hatchet(debug=False)
# > Streaming
anna_karenina = """
Happy families are all alike; every unhappy family is unhappy in its own way.
Everything was in confusion in the Oblonskys' house. The wife had discovered that the husband was carrying on an intrigue with a French girl, who had been a governess in their family, and she had announced to her husband that she could not go on living in the same house with him.
"""
def create_chunks(content: str, n: int) -> Generator[str, None, None]:
for i in range(0, len(content), n):
yield content[i : i + n]
chunks = list(create_chunks(anna_karenina, 10))
@hatchet.task()
async def stream_task(input: EmptyModel, ctx: Context) -> None:
# 👀 Sleeping to avoid race conditions
await asyncio.sleep(2)
for chunk in chunks:
await ctx.aio_put_stream(chunk)
await asyncio.sleep(0.20)
def main() -> None:
worker = hatchet.worker("test-worker", workflows=[stream_task])
worker.start()
if __name__ == "__main__":
main()