Files
hatchet/python-sdk/examples/manual_trigger/worker.py
Gabe Ruttner d8b6843dec feat: streaming events (#309)
* feat: add stream event model

* docs: how to work with db models

* feat: put stream event

* chore: rm comments

* feat: add stream resource type

* feat: enqueue stream event

* fix: contracts

* feat: protos

* chore: set properties correctly for typing

* fix: stream example

* chore: rm old example

* fix: async on

* fix: bytea type

* fix: worker

* feat: put stream data

* feat: stream type

* fix: correct queue

* feat: streaming payloads

* fix: cleanup

* fix: validation

* feat: example file streaming

* chore: rm unused query

* fix: tenant check and read only consumer

* fix: check tenant-steprun relation

* Update prisma/schema.prisma

Co-authored-by: abelanger5 <belanger@sas.upenn.edu>

* chore: generate protos

* chore: rename migration

* release: 0.20.0

* feat(go-sdk): implement streaming in go

---------

Co-authored-by: gabriel ruttner <gabe@hatchet.run>
Co-authored-by: abelanger5 <belanger@sas.upenn.edu>
2024-04-01 15:46:21 -04:00

51 lines
1.4 KiB
Python

from hatchet_sdk import Hatchet, Context
from dotenv import load_dotenv
import base64
import os
load_dotenv()
hatchet = Hatchet(debug=True)
@hatchet.workflow(on_events=["man:create"])
class ManualTriggerWorkflow:
@hatchet.step()
def step1(self, context: Context):
res = context.playground('res', "HELLO")
# Get the directory of the current script
script_dir = os.path.dirname(os.path.abspath(__file__))
# Construct the path to the image file relative to the script's directory
image_path = os.path.join(script_dir, "image.jpeg")
# Load the image file
with open(image_path, "rb") as image_file:
image_data = image_file.read()
print(len(image_data))
# Encode the image data as base64
base64_image = base64.b64encode(image_data).decode('utf-8')
# Stream the base64-encoded image data
context.put_stream(base64_image)
context.sleep(3)
print("executed step1")
return {"step1": "data1 "+res}
@hatchet.step(parents=["step1"], timeout='4s')
def step2(self, context):
print("started step2")
context.sleep(1)
print("finished step2")
return {"step2": "data2"}
workflow = ManualTriggerWorkflow()
worker = hatchet.worker('manual-worker', max_runs=4)
worker.register_workflow(workflow)
worker.start()