Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New engine: add support for sync and async generator tasks #13138

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from

Conversation

jlowin
Copy link
Member

@jlowin jlowin commented Apr 27, 2024

This PR builds on #12889 and #12915 to add native support for generator tasks to the new engine, both sync and async. It can be considered a draft until those are finalized. In Prefect 2.x you can decorate generators as tasks, but they are not preserved as generators. Instead, when the task is called the generator is fully consumed and the resulting list is returned as the task's result. This PR preserves true generator semantics. A task-decorated generator is still a "true" generator when called, and must be iterated over to produce values incrementally. The same goes for async generators.

Generator tasks inherit all properties of regular tasks, including retries.

This PR also enhances dependency tracking to automatically mark generators as "parent" tasks of any tasks that consume the values they yield.

import random
from prefect import task, flow

@task
def gen_numbers():
    yield from range(10)

@task
def process_number(n: int):
    print(n)

@task
def show_status():
    print(random.choice(["👋", "🧐", "🤬", "💀"]))

@flow
def demo():
    for n in gen_numbers():
        process_number(n)
        show_status()


demo() 
0
👋
1
👋
2
💀
3
🧐
4
💀
5
🤬
6
👋
7
👋
8
🤬
9
👋

This PR does not add support for generator flows.

There are now four distinct execution functions (run_task, run_task_sync, run_task_generator, and run_task_generator_sync) with almost identical logic, apart from syntactical requirements (like awaits and yields). DRYing them out would have certainly created conflicts with other PRs, so I am saving that optimization for once these are more dialed in.

@jlowin jlowin requested a review from a team as a code owner April 27, 2024 15:20
@jlowin jlowin marked this pull request as draft April 27, 2024 15:21
Copy link

netlify bot commented Apr 27, 2024

Deploy Preview for prefect-docs-preview ready!

Name Link
🔨 Latest commit ba2a11d
🔍 Latest deploy log https://app.netlify.com/sites/prefect-docs-preview/deploys/662f010bdbd3a200088b30bb
😎 Deploy Preview https://deploy-preview-13138--prefect-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@jlowin jlowin changed the title Add support for sync and async generator tasks Add support for sync and async generator tasks (new engine) Apr 27, 2024
@jlowin jlowin changed the title Add support for sync and async generator tasks (new engine) New engine: add support for sync and async generator tasks Apr 28, 2024
@jlowin jlowin added the orchestration Related to Orchestration features label Apr 28, 2024
Copy link
Contributor

This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment.

@github-actions github-actions bot added the status:stale This may not be relevant anymore label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
orchestration Related to Orchestration features status:stale This may not be relevant anymore
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant