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

Add support for generator tasks #13820

Merged
merged 11 commits into from
Jun 11, 2024
Merged

Add support for generator tasks #13820

merged 11 commits into from
Jun 11, 2024

Conversation

jlowin
Copy link
Member

@jlowin jlowin commented Jun 5, 2024

This PR leverages #13793 to add native support for generator tasks to the new engine, both sync and async. 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 generator must be iterated over to produce values incrementally. The same goes for async generators.

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

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

This PR does not add support for generator flows.

Here is a simple demo that shows generator yields interspaced with dependent task run executions:

import random
from prefect import task, flow

@task
def gen_numbers():
    for i in range(10):
        yield i
        print(f'yielded {i}')
    

@task
def process_number(n: int):
    print(f'got {n}')

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

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


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

values.append(v)
except ValueError:
pass
assert values == [1, 2, 1, 2, 1, 2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

Copy link
Member

@cicdw cicdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Copy link
Collaborator

@zzstoatzz zzstoatzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔋

src/prefect/tasks.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@chrisguidry chrisguidry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the tests for me

@jlowin jlowin merged commit 7ef31fe into main Jun 11, 2024
26 checks passed
@jlowin jlowin deleted the generators-3 branch June 11, 2024 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants