diff --git a/src/content/docs/workflows/python/bindings.mdx b/src/content/docs/workflows/python/bindings.mdx index 75983d200d0b9d2..1eefc73f71ec484 100644 --- a/src/content/docs/workflows/python/bindings.mdx +++ b/src/content/docs/workflows/python/bindings.mdx @@ -16,7 +16,7 @@ You must add both `python_workflows` and `python_workers` compatibility flags to Also, Python Workflows requires `compatibility_date = "2025-08-01"`, or lower, to be set in your `wrangler.toml` file. ::: -The Python Workers platform leverages FFI to access bindings to Cloudflare resources. Refer to the [bindings](/workers/languages/python/ffi/#using-bindings-from-python-workers) documentation for more information. +The Python Workers platform leverages [FFI](https://en.wikipedia.org/wiki/Foreign_function_interface) to access bindings to Cloudflare resources. Refer to the [bindings](/workers/languages/python/ffi/#using-bindings-from-python-workers) documentation for more information. From the configuration perspective, enabling Python Workflows requires adding the `python_workflows` compatibility flag to your `wrangler.toml` file. diff --git a/src/content/docs/workflows/python/dag.mdx b/src/content/docs/workflows/python/dag.mdx index 194e326a289b017..30974627214203c 100644 --- a/src/content/docs/workflows/python/dag.mdx +++ b/src/content/docs/workflows/python/dag.mdx @@ -9,26 +9,38 @@ sidebar: The Python Workflows SDK supports DAG workflows in a declarative way, using the `step.do` decorator with the `depends` parameter to define dependencies (other steps that must complete before this step can run). ```python -from workers import WorkflowEntrypoint +from workers import Response, WorkflowEntrypoint -class MyWorkflow(WorkflowEntrypoint): +class PythonWorkflowStarter(WorkflowEntrypoint): async def run(self, event, step): - @step.do("dependency a") - async def step_a(): - # do some work - return 10 - - @step.do("dependency b") - async def step_b(): - # do some work - return 20 - - @step.do("my final step", depends=[step_a, step_b], concurrent=True) - async def my_final_step(result_a=0, result_b=0): - # should return 30 - return result_a + result_b - - await my_final_step() + async def await_step(fn): + try: + return await fn() + except TypeError as e: + print(f"Successfully caught {type(e).__name__}: {e}") + + step.sleep('demo sleep', '10 seconds') + + @step.do('dependency1') + async def dep_1(): + # does stuff + print('executing dep1') + + @step.do('dependency2') + async def dep_2(): + # does stuff + print('executing dep2') + + @step.do('demo do', depends=[dep_1, dep_2], concurrent=True) + async def final_step(res1, res2): + # does stuff + print('something') + + await await_step(final_step) + +async def on_fetch(request, env): + await env.MY_WORKFLOW.create() + return Response("Hello world!") ``` On this example, `step_a` and `step_b` are run concurrently before execution of `my_final_step`, which depends on both of them. diff --git a/src/content/docs/workflows/python/index.mdx b/src/content/docs/workflows/python/index.mdx index ee5964030e494a7..3efbc1fdbc5de50 100644 --- a/src/content/docs/workflows/python/index.mdx +++ b/src/content/docs/workflows/python/index.mdx @@ -12,12 +12,7 @@ Workflow entrypoints can be declared using Python. To achieve this, you can expo Refer to [Python Workers](/workers/languages/python) for more information about Python on the Workers runtime. :::caution[Python Workflows are in beta, as well as the underlying platform.] - -You must add both `python_workflows` and `python_workers` compatibility flags to your `wrangler.toml` file. - -Also, Python Workflows requires `compatibility_date = "2025-08-01"`, or lower, to be set in your `wrangler.toml` file. - -Join the #python-workers channel in the [Cloudflare Developers Discord](https://discord.cloudflare.com/) and let us know what you'd like to see next. +Python Workflows requires `compatibility_date = "2025-08-01"`, or lower, to be set in your `wrangler.toml` file. ::: ## Get Started @@ -32,6 +27,45 @@ class MyWorkflow(WorkflowEntrypoint): # steps here ``` +For example, a Workflow may be defined as: + +```python +from workers import Response, WorkflowEntrypoint + +class PythonWorkflowStarter(WorkflowEntrypoint): + async def run(self, event, step): + + @step.do('step1') + async def step_1(): + # does stuff + print('executing step1') + + @step.do('step2') + async def step_2(): + # does stuff + print('executing step2') + + await await_step(step_1,step_2) + +async def on_fetch(request, env): + await env.MY_WORKFLOW.create() + return Response("Hello world!") + ``` + +You must add both `python_workflows` and `python_workers` compatibility flags to your `wrangler.toml` file. + +``` +name = "hello-python" +main = "src/entry.py" +compatibility_flags = ["python_workers", "experimental", "python_workflows"] +compatibility_date = "2024-03-29" + +[[workflows]] +name = "workflows-demo" +binding = "MY_WORKFLOW" +class_name = "PythonWorkflowStarter" +``` + To run a Python Workflow locally, you use [Wrangler](/workers/wrangler/), the CLI for Cloudflare Workers: ```bash @@ -42,4 +76,6 @@ To deploy a Python Workflow to Cloudflare, run [`wrangler deploy`](/workers/wran ```bash npx wrangler@latest deploy -``` \ No newline at end of file +``` + +Join the #python-workers channel in the [Cloudflare Developers Discord](https://discord.cloudflare.com/) and let us know what you'd like to see next.