mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-01-04 15:52:25 -06:00
feat(py-sdk): async steps (#246)
* feat: async wrapper * feat: async examples * release: bump version 0.15.1
This commit is contained in:
13
python-sdk/examples/async/event_test.py
Normal file
13
python-sdk/examples/async/event_test.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from hatchet_sdk import new_client
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
client = new_client()
|
||||
|
||||
client.event.push(
|
||||
"user:create",
|
||||
{
|
||||
"test": "test"
|
||||
}
|
||||
)
|
||||
36
python-sdk/examples/async/worker.py
Normal file
36
python-sdk/examples/async/worker.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from hatchet_sdk import Hatchet, Context
|
||||
from dotenv import load_dotenv
|
||||
import asyncio
|
||||
|
||||
load_dotenv()
|
||||
|
||||
hatchet = Hatchet(debug=True)
|
||||
|
||||
@hatchet.workflow(on_events=["user:create"])
|
||||
class AsyncWorkflow:
|
||||
def __init__(self):
|
||||
self.my_value = "test"
|
||||
|
||||
@hatchet.step(timeout='5s')
|
||||
def step1(self, context: Context):
|
||||
async def async_step1():
|
||||
print("started step1")
|
||||
await asyncio.sleep(2)
|
||||
print("finished step1")
|
||||
return 'result'
|
||||
|
||||
res = asyncio.run(async_step1())
|
||||
print(res)
|
||||
return {'test': 'test'}
|
||||
|
||||
@hatchet.step(parents=["step1"],timeout='4s')
|
||||
async def step2(self, context):
|
||||
print("started async step2")
|
||||
await asyncio.sleep(2)
|
||||
print("finished step2")
|
||||
|
||||
workflow = AsyncWorkflow()
|
||||
worker = hatchet.worker('test-worker', max_runs=4)
|
||||
worker.register_workflow(workflow)
|
||||
|
||||
worker.start()
|
||||
@@ -1,5 +1,7 @@
|
||||
from .client import new_client
|
||||
from typing import List
|
||||
import asyncio
|
||||
from functools import wraps
|
||||
from .workflow import WorkflowMeta
|
||||
from .worker import Worker
|
||||
from .logger import logger
|
||||
@@ -38,14 +40,20 @@ class Hatchet:
|
||||
|
||||
return inner
|
||||
|
||||
def step(self, name : str='', timeout : str='', parents : List[str] = [], retries : int = 0):
|
||||
def step(self, name: str='', timeout: str='', parents: List[str] = [], retries: int = 0):
|
||||
def inner(func):
|
||||
func._step_name = name or func.__name__
|
||||
func._step_parents = parents
|
||||
func._step_timeout = timeout
|
||||
func._step_retries = retries
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
return asyncio.run(func(*args, **kwargs))
|
||||
else:
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return func
|
||||
wrapper._step_name = name or func.__name__
|
||||
wrapper._step_parents = parents
|
||||
wrapper._step_timeout = timeout
|
||||
wrapper._step_retries = retries
|
||||
return wrapper
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "hatchet-sdk"
|
||||
version = "0.15.0"
|
||||
version = "0.15.1"
|
||||
description = ""
|
||||
authors = ["Alexander Belanger <alexander@hatchet.run>"]
|
||||
readme = "README.md"
|
||||
|
||||
Reference in New Issue
Block a user